My local development script uses proxying to get around CORS (among other things). I first go get some false cookies which allow me to skip authentication in my app while developing. The callback to spoofCookies then set up my static proxy - which delivers all my local static content, and then sets up a proxy for all calls routed to /microsvc. I take that call, inject my spoofed cookies, and forward it to the same path on a remote host.
This script works great for GET but I am stuck for how to write the POST section. The req never shows the POST form I have sent through my proxy and so I must be doing something really wrong.
Thanks for your time
/*jslint node:true white:true nomen:true es5:true*/
'use strict';
var PORT_HTTP = process.env.PORT || 4400;
var localAddress = "http://localhost:" + PORT_HTTP;
var apiAddress = { label: "On-site Ethernet Plug",
protocol: "http://",
port: 80,
host : "10.17.100.11",
microsvc: "/microsvc",
auth: "/login.form"
};
var https = require('https');
var path = require('path');
var express = require('express');
var app = express();
var spoofCookie = require('./utils/spoof-cookies');
var cookies = [];
var proxy = function(myPath, searchOptions, req, res, cookies) {
var options = {
hostname: apiAddress.host,
port: apiAddress.port,
path: apiAddress.microsvc + searchOptions,
rejectUnauthorized: false
};
if(cookies.length){
options.headers = {
"Cookie":cookies[0]
};
}
try {
console.log('FROM: ' + localAddress + myPath + searchOptions + '\n' +
'↳ TO: ' + apiAddress.protocol + apiAddress.host + apiAddress.microsvc + searchOptions);
options.method = req.method;
if(options.method == 'POST' || options.method == 'PUT') {
// HELP
// form data never appears in output
console.log(req);
} else {
var r = https.request(options, function(resX) {
resX.setEncoding('utf8');
var responseData = '';
resX.on('data', function(chunk) {
responseData += chunk;
});
resX.on('end', function() {
res.send(JSON.parse(responseData));
});
});
r.on('error', function(e) {
if (e.code == "ENOTFOUND") {
console.error("File not found on remote");
} else {
console.error('ff ' + e);
}
});
}
req.pipe(r).pipe(res);
} catch (e) {
console.error(e);
}
};
// START - first get spoofed cookies
spoofCookie.getCookies(apiAddress, function(cookies) {
// set up server
var httpServer = http.createServer(app);
httpServer.listen(PORT_HTTP);
// microservice proxy
app.use('/microsvc', function (req, res) {
proxy('/microsvc', req.url, req, res, cookies);
});
console.log("HTTP server started: " + localAddress);
});
// static server runs anything that isn't set up for proxy
app.use(express.static(path.resolve(path.join(__dirname, "..", "app"))));
Express is not parsing request body by default.
req.body
contains key-value pairs of data submitted in the request body. By default, it isundefined
, and is populated when you use body-parsing middleware such as body-parser and multer .
So, the setup with body-parser
would look like so.
First, attach middleware somewhere before declaring routes:
var bodyParser = require('body-parser');
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }));
// parse application/json
app.use(bodyParser.json());
// microservice proxy
app.use('/microsvc', function (req, res) {
proxy('/microsvc', req.url, req, res, cookies);
});
Then, access request content in request.body
:
if(options.method == 'POST' || options.method == 'PUT') {
console.log(req.body);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.