Making a proxy with request and express in node - node.js

I'm trying to make a proxy with the express and request module. This post shows how to easily pipe() the request to the response:
app.use(express.json()); // to support JSON-encoded bodies
app.use(express.urlencoded()); // to support URL-encoded bodies
//app.use(express.multipart());
app.use('/api', function(req, res) {
var url =proxyUrl + req.url;
req.pipe(request(url)).pipe(res);
});
It works very well for all GET request. But on a POST request it fails. Can't figure out what is happening because it only runs into a timeout. The POST request works on the proxy.
What am I doing wrong? Is there any changes that I can debug the request? I have tried the following, but the file is empty:
req.pipe(fs.createWriteStream("test.txt"));

We are also experiencing same issues, its because express.json(). After disabling json parser, it work ok. But that not the idle solution as you might need json parser.

Related

Body parsers behaving weirdly

I am trying to develop an app with few APIs, the issue I am facing here is when I use
app.use(bodyParser.urlencoded({ extended: false }));
as my middleware, it doesnt intake the requests i send through postman (all the request sent through postman goes with empty body idk why. And it takes all the requests sent via html form.
On the other hand, if I use
app.use(express.json({extended: false}))
as my middleware to parse json objects, it takes all the requests from postman but doesnt take requests from my browser form. Can anyone explain whats happening here?
In order for express to be able to parse both JSON request payloads and simple form-data requests, you simply need to setup both of the mentioned middlewares (note that express.json() does not have an extended option):
app.use(bodyParser.urlencoded({ extended: false }));
app.use(express.json());
See the docs for more information:
https://expressjs.com/en/api.html#express.json
https://expressjs.com/en/api.html#express.urlencoded

Express Application in AWS Lambda has base64 encoded body every single time

I'm making post requests to an AWS Api gateway with a lambda proxy integration pointing to a lambda function. This lambda function is a wrapper for an existing express application. I could decode the body myself and set the body to be appropriately formatted, but I don't have to do this when I run the express server manually. I'm essentially trying to avoid adding middleware that checks if it's a lambda function, then setting the body to be the decoded JSON of the body. I thought this would be an easy fix (and it probably is), but I'm not sure how to do it.
All requests are made from Postman with x-www-form-urlencoded.
I've tried messing around with some of the body parsers, binarymimetypes, doing application/json on postman, using awsserverless event context middleware.
app.set('view engine', 'ejs');
app.use(bodyParser.urlencoded({
extended: true,
}));
app.use(bodyParser.json());
app.use((request, response, next) => {
console.log("GOT BODY!");
console.log(request.body);
next();
});
The above code prints the base64 encoded value if the app is on aws lambda and prints the correct JSON value if running locally.

Setting the body of a http-proxy-middleware Proxied Request

I'm still new still newish to node, and having some troubling figuring out how to set a new response body with the http-proxy-middleware package.
That is, I have the following small program that proxies requests
var express = require('express');
var proxy = require('http-proxy-middleware');
var app = express();
app.use('**', proxy({
target: 'http://alanstorm.com',
changeOrigin: true,
onProxyRes:function (proxyRes, req, res) {
//I want to do something here to change the response
console.log("Called");
console.log(proxyRes);
console.log(res);
}
}));
app.listen(3000);
i.e. -- I can make a request to http://localhost:3000, and it will proxy the request to my personal website.
I've also successfully setup a response listener (onProxyRes) above. I seem to have access to a proxy response object, a request object, and a response object. What I'd like to do is, in the onProxyRes method, is change the response if certain things are true or not.
However, it's not clear how to do this with the proxyRes or res objects, and I'm not sure how to look up the available methods on these objects. I tried console.loging them for useful properties, and found nothing useful.
If someone know how i can modify the bodies of the response objects, that would be great. If someone can tell me how I can figure out what methods exist on those objects, that would be extra great.

Can't POST (and parse) params to app.post() in express.js

I checked this question but for some reasons the solution given is not working for me. In my express.js I have:
...
bodyParser = require('body-parser')
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(bodyParser.json());
...
Run test using POSTman
app.post('/upload', function(req, res) {
console.log(req.body) // undefined
console.log(req.params) // undefined
})
and the result:
So both body & params are empty. Any suggestions?
The reason the solution in the link you provided doesn't work is because that version of body-parser is out of date and doesn't include form-data parsing anymore (and it used to be bundled with express).
With that being said, based on your screenshot, it looks like you are sending data of type multipart/form-data(you can check this in the request headers) to your server and your code sample only shows middleware that handles urlencoded and json data types.
You need to add middleware that handles that data type. The latest body parser says (https://github.com/expressjs/body-parser):
This does not handle multipart bodies, due to their complex and
typically large nature. For multipart bodies, you may be interested in
the following modules:
busboy and connect-busboy
multiparty and connect-multiparty
formidable
multer
So check out one of the above parsers. I use busboy and it works great.

How does one retrieve the entity body from a request in Node.js/Express (for CSP violation reporting)?

I am interested in implementing a Content Security Policy (CSP) for my Node.js application. Mozilla's docs are rather helpful but I am stuck at how to enable violation reports. I understand the basic premise of how they work (the browser sends a POST request to the specified URL to notify the website of a violation) but could not figure out where to find the JSON document describing the violation in the HTTP request. Perhaps this would have been obvious to someone more familiar with the HTTP spec.
Looking at the W3C draft for CSP, I established that the JSON is contained in a portion of the HTTP called the "entity body". I still don't know what the purpose of the entity is (the only mildly useful page I could find on the matter was one from the HTTP spec). I am assuming it the body of the request.
Perhaps more importantly, I cannot find any way to retrieve the contents of the entity body. I thought of using req.header('entity-body') but that doesn't work as the entity is not a HTTP header. What is it and how to I fetch it?
(Additionally, I tried finding a tutorial on how to implement CSP violation reporting in Node.js and found nothing. I did find one for PHP but it wasn't particularly helpful, referencing a file_get_contents('php://input') which I don't have anything similar to in Node.js/Express.)
Any assistance would be greatly appreciated.
It turns out I was over-analyzing things. All you need to do is enable the express.bodyParser() middleware for express and then fetch req.body in the POST event handler. This retrieves the body of the HTTP request containing the JSON violation report.
Enable middleware:
var server = express.createServer(
// other middleware here
express.bodyParser()
);
Retrieving violation report:
server.post('/csp/', function(req, res) {
console.log(req.body);
});
I went through some difficulties getting my Express app fronted with Nginx to report csp violations and the two things I learned from the above answer were:
Should be POST method and not GET method
req.body contains the report
But, the above was not sufficient and I kept getting empty req.body and I could not find any other post to describe how to fix it. After some research I came across this post as well as a totally isolated github issue where dougwilson give hints where to put the route that handles the csp report.
The reason the req.body was empty for me was because I placed the csp report route handler after the following configs:
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(cookieParser());
I moved the csp report route handler above these but, I still kept getting empty req.body then I added the following above csp report route handler to get the report in req.body
app.use(bodyParser.json({ type: 'application/csp-report' }));
After adding the above line above csp report request handler, Express understood that it should parse requests that have Content-type as application/csp-report.
Maybe Express by default do not parse application/csp-report, and adding the above resolved the issue for me. I also googled if Express parses application/csp-report by default and I came across this gist claiming that Chrome sends application/csp-report whereas Firefox sends application/json (and I am using Chrome - you can include application/json also if you face issues with FF).
So this is how it looks in my app.js
// without following csp-report don't get parsed.
app.use(bodyParser.json({ type: 'application/csp-report' }));
app.get('/vehicle/cspreport', function(req, res) {
res.status(403);
});
app.post('/vehicle/cspreport', function(req, res) {
console.log('csp report > ' + JSON.stringify(req.body));
});
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(cookieParser());
...
The accepted answer posted by OP is from 2011 and I thought of adding an answer to show how I resolved this issue in 2016 with the following versions of Node.js, Express and Nginx
Node: v4.2.4
Express: 4.13.1
Nginx: 1.8.1

Resources