36 lines
1.1 KiB
JavaScript
36 lines
1.1 KiB
JavaScript
var http = require('http');
|
|
var httpProxy = require('http-proxy');
|
|
var modifyResponse = require('../');
|
|
|
|
// Create a proxy server
|
|
var proxy = httpProxy.createProxyServer({
|
|
target: 'http://localhost:9999'
|
|
});
|
|
|
|
const reg = new RegExp('^/(rest|auth|api|user|userData)')
|
|
const isObject = function (obj) {
|
|
return Object.prototype.toString.call(obj) === '[object Object]';
|
|
}
|
|
// Listen for the `proxyRes` event on `proxy`.
|
|
proxy.on('proxyRes', function (proxyRes, req, res) {
|
|
if (reg.test(req.url)) {
|
|
console.log(req.url)
|
|
modifyResponse(res, proxyRes, function (body) {
|
|
console.log(JSON.stringify(body))
|
|
if (body && body.notify === false) {
|
|
body.notify = true
|
|
}
|
|
return body; // return value can be a promise
|
|
});
|
|
}
|
|
});
|
|
|
|
// Create your server and then proxies the request
|
|
var server = http.createServer(function (req, res) {
|
|
proxy.web(req, res);
|
|
}).listen(9998);
|
|
|
|
// Create your target server
|
|
// var targetServer = http.createServer(function (req, res) {
|
|
// res.end();
|
|
// }).listen(9999);
|