node.js & webpack proxy bug
node.js & webpack proxy bug
[HPM] Error occurred while trying to proxy request /express/api/post from 10.1.159.45:8088 to http://localhost (ECONNREFUSED) (https://nodejs.org/api/errors.html#errors_common_system_errors)
ECONNREFUSED
econnrefused
拒绝
devServer: {
hot: true,
contentBase: './src',
historyApiFallback: true,
noInfo: true,
host: devServerHost,
port: devServerPort,
publicPath: '/',
proxy: [
{
context: [['/api', '/version', '/qywx/'],
// 测试环境
target: 'http://app-test.xgqfrms.xyz',
// 灰度环境
// target: 'http://app-pre.xgqfrms.xyz',
//生成环境
// target: 'https://app.xgqfrms.xyz',
changeOrigin: true,
onError (err) {
console.log('proxy error:', err);
}
},
{
// context: ['/mock/'],
context: ['/express/'],
target: 'http://localhost',
rewrite: function(req) {
req.url = req.url.replace(/^\/express/, '');
},
port: 9000,
changeOrigin: true,
},
],
clientLogLevel: 'warning',
overlay: { warnings: false, errors: true },
watchOptions: { poll: false }
},
https://nodejs.org/api/errors.html#errors_common_system_errors
const bodyParser = require('body-parser');
const express = require('express');
const fs = require('fs');
const hostname = 'localhost';
// const hostname = '127.0.0.1';
// const hostname = 'http://10.1.159.45';
// const port = 3000;
const port = 9000;
const app = express();
// Starting with release 4.16.0, a new express.json() middleware is available.
app.use(express.json());
app.use(express.urlencoded({
extended: true,
}));
app.use(function (req, res, next) {
// JSON parse
// console.log('req.body', req.body);
// CORS bug
res.header("Access-Control-Allow-Origin", "*");
// res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
// res.header("Content-Security-Policy", "connect-src *");
// res.header("Content-Security-Policy", "connect-src '*'");
// res.header("Content-Security-Policy", "connect-src 'localhost'");
// res.header("Content-Security-Policy", "connect-src localhost");
// Content-Security-Policy: connect-src <source>;
// Content-Security-Policy: connect-src <source> <source>;
// res.header('Content-Type', 'application/json');
// res.setHeader('Content-Type', 'application/json');
next();
});
// https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=555a887d-c247-4808-a850-22440c5b25a0
app.get('/api/get', (req, res) => {
// console.log('get req', req);
console.log('req.params', req.params);
console.log('req.query', req.query);
// console.log('req.query', req.query, JSON.stringify(JSON.parse(req.query), null, 4));
const obj = JSON.parse(req.query.q);
console.log('obj =', JSON.stringify(obj, null, 4));
res.setHeader('Content-Type', 'text/html');
res.send('get api');
});
app.post('/api/post', (req, res) => {
// console.log('❌ post req', req);
console.log('✅ post req.body', req.body);
// res.setHeader('Content-Type', 'application/json');
// res.sendStatus(200);
res.send({res: 'post api'});
});
// app.post('/api/beacon', (req, res) => {
// // console.log('❌ beacon req', req);
// console.log('✅ beacon post req.body', req.body);
// // res.setHeader('Content-Type', 'application/json');
// // res.sendStatus(200);
// res.send({res: 'beacon api'});
// });
app.listen(port, () => {
console.log(`
Server is running at http://${hostname}:${port}/
Server hostname ${hostname} is listening on port ${port}!
`);
});
/*
http://localhost:9000/api/get?q=%7B%22username%22:%22xgqfrms%22%7D
fetch(`http://localhost:9000/api/post`, {
body: JSON.stringify({key: "value"}),
cache: "no-cache",
headers: {
"Content-Type": "application/json",
},
method: "POST",
// mode: "no-cors",
mode: "cors",
})
.then(res => console.log(`res =`, res))
.catch(err => console.error(`error =`, err));
http://localhost:3000/api/get?q={%22username%22:%22xgqfrms%22}
fetch(`http://localhost:3000/api/post`, {
body: JSON.stringify({key: "value"}),
cache: "no-cache",
headers: {
"Content-Type": "application/json",
},
method: "POST",
// mode: "no-cors",
mode: "cors",
})
.then(res => console.log(`res =`, res))
.catch(err => console.error(`error =`, err));
fetch(`http://127.0.0.1:3000/api/post`, {
body: JSON.stringify({key: "value"}),
cache: "no-cache",
headers: {
"Content-Type": "application/json",
},
method: "POST",
// mode: "no-cors",
mode: "cors",
})
.then(res => console.log(`res =`, res))
.catch(err => console.error(`error =`, err));
*/
refs
©xgqfrms 2012-2020
www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!
原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!
本文首发于博客园,作者:xgqfrms,原文链接:https://www.cnblogs.com/xgqfrms/p/14624627.html
未经授权禁止转载,违者必究!