Receiving POST data with Express.js sent by angular2 app - node.js

I have following code on Node.js:
app.post('/register', function(req, res) {
console.log(req.body);
But it appears that req object does not have body property.
With angular2 I am sending stringified JSON with Content-Type:application/json
Angular code looks like this:
this.http.post(
"http://url.com",
JSON.stringify(data_obj), {headers:{'Content-Type':
'application/json'}}).subscribe((res:Response) => this.extractData(res));
Thanks

The NodeJS application does not have bodyParser so it is unable to parse the body of the request. Do this in your NodeJS application
Install body-parser
npm install body-parser --save
Add to nodeJS app
var express = require('express')
var bodyParser = require('body-parser')
var app = express()
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }))

You have to use the body parser middleware in your app:
https://github.com/expressjs/body-parser
you can install it with this command: npm install body-parser --save
for example you can write this code to your main file:
let bodyParser = require('body-parser')
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: false }))
This is a basic configuration for the body parser middleware, it parse POST parameters into req.body.
If you want to learn more about the configurations of body parser, read the associate doc into the github repo.

Related

I have problems to run server.js in terminal

enter image description here[
// Setup empty JS object to act as endpoint for all routes
projectData = {};
// Require Express to run server and routes
const express = require('express');
const app = express();
// Start up an instance of app
const PORT = 8080;
app.listen(PORT, () => {
console.log('Hi');
console.log(`the port that we will use is ${port}`);
});
/* Middleware*/
//Here we are configuring express to use body-parser as middle-ware.
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
// Cors for cross origin allowance
app.use(cors());
// Initialize the main project folder
app.use(express.static('website'));
// Setup Server
app.post('/link', function(req,res) {
});
What should i do to run this in terminal
*I tried alot of solutions bot it's not working
in the terminal[
1.
it can not find the file
]3*
I think you need to install the 'body-parser' library and send it to call in your file
npm install body-parser
const bodyParser = require('body-parser');
app.use(bodyParser.json());
You don't need to use bodyparser if you use latest or after 4.0 version of Express.js. You can use
app.use(express.json()) // Parse Json Bodies
app.use(express.urlencoded()); //Parse URL-encoded bodies
as middlewares instead, without any package. They will solve your problem.

why do we need body parser in an express application?

I don't understand why we need body-parser in an Express application, as we can get data without using body-parser. And what does it do actually and how?
The latest version of express doesn't require setting up body-parser as it is inbuilt in to express.
see this PR for more details
Formally we use body parser to handle incoming request bodies. request like JSON, urlencoded etc but now team behind express has added it as a global core module, so you dont need to install/import body parser again
instead of doing
import bodyparser = require("body-parser")
import express = require("express")
const app = express();
app.use(bodyparser.json());
app.use(bodyparser.urlencoded({extended: true}))
You Can now do
import express = require("express")
const app = express();
app.use(express.json());
app.use(express.urlencoded({extended: true}))

Express: Req.body is undefined after POST req

I am new to node JS.
I am getting undefined for post request. My express version is 4.10.
I think I am missing something.
var express = require('express');
var http = require('http');
var app = express();
app.use(express.static(__dirname + '/public'));
var bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());
app.post('/test',function(req,res){
var color1 = req.body.color;
console.log(req.headers);
console.log("Color : "+color1);
});
In content-length I am getting 234.
Thanks!!
For future visitors - it seems that #mscdex's suggestion lead #Rahul to change the client calling his API so that it passed application/json as the value for the Content-Type header. Another option is to change the Content-Type header that body-parser attempts to parse.
You can configure body-parser to accept a different Content-Type by specifying the type it accepts as follows:
app.use(bodyParser.json({ type: 'application/vnd.api+json' }));
This is the solution that worked for me in order to parse the JSON sent from an Ember app. I felt it was better to change the default Content-Type header accepted by body-parser than changing the rest of the tooling around my application.
The correct answer is to change the default Content-Type as described above:
app.use(bodyParser.json({
type: 'application/vnd.api+json',
strict: false
}));
strict:false gets around some bugs in bodyParser rejecting valid JSON objects.
npm install body-parser
However, if you are running a version of Express that is 4.16+, it now includes the same functionality inside of Express.
Instead of adding these lines in the code to get req.body:
app.use(bodyparser.urlencoded({extended: false}));
app.use(bodyparser.json());
If you are using Express 4.16+ you can now replace that with:
app.use(express.urlencoded({extended: false}));
app.use(express.json());

bodyParser is deprecated express 4

I am using express 4.0 and I'm aware that body parser has been taken out of the express core, I am using the recommended replacement, however I am getting
body-parser deprecated bodyParser: use individual json/urlencoded middlewares server.js:15:12
body-parser deprecated urlencoded: explicitly specify "extended: true" for extended parsing node_modules/body-parser/index.js:74:29
Where do I find this supposed middlewares? or should I not be getting this error?
var express = require('express');
var server = express();
var bodyParser = require('body-parser');
var mongoose = require('mongoose');
var passport = require('./config/passport');
var routes = require('./routes');
mongoose.connect('mongodb://localhost/myapp', function(err) {
if(err) throw err;
});
server.set('view engine', 'jade');
server.set('views', __dirname + '/views');
server.use(bodyParser());
server.use(passport.initialize());
// Application Level Routes
routes(server, passport);
server.use(express.static(__dirname + '/public'));
server.listen(3000);
It means that using the bodyParser() constructor has been deprecated, as of 2014-06-19.
app.use(bodyParser()); //Now deprecated
You now need to call the methods separately
app.use(bodyParser.urlencoded());
app.use(bodyParser.json());
And so on.
If you're still getting a warning with urlencoded you need to use
app.use(bodyParser.urlencoded({
extended: true
}));
The extended config object key now needs to be explicitly passed, since it now has no default value.
If you are using Express >= 4.16.0, body parser has been re-added under the methods express.json() and express.urlencoded().
Want zero warnings? Use it like this:
// Express v4.16.0 and higher
// --------------------------
const express = require('express');
app.use(express.json());
app.use(express.urlencoded({
extended: true
}));
// For Express version less than 4.16.0
// ------------------------------------
const bodyParser = require('body-parser');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
Explanation: The default value of the extended option has been deprecated, meaning you need to explicitly pass true or false value.
Note for Express 4.16.0 and higher: body parser has been re-added to provide request body parsing support out-of-the-box.
If you're using express > 4.16, you can use express.json() and express.urlencoded()
The express.json() and express.urlencoded() middleware have been added to provide request body parsing support out-of-the-box. This uses the expressjs/body-parser module module underneath, so apps that are currently requiring the module separately can switch to the built-in parsers.
Source Express 4.16.0 - Release date: 2017-09-28
With this,
const bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
becomes,
const express = require('express');
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
Don't use body-parser
If you are using Express 4.16+ You can do it just like this with express:
app.use(express.urlencoded({extended: true}));
app.use(express.json()) // To parse the incoming requests with JSON payloads
You can now uninstall body-parser using npm uninstall body-parser
To get the POST content, you can use req.body
app.post("/yourpath", (req, res)=>{
var postData = req.body;
//Or if this doesn't work
var postData = JSON.parse(req.body);
});
I hope this helps
Even I faced the same issue. The below change I mentioned resolved my problem.
If you're using Express 4.16+ version, then
You may have added a line to your code that looks like the following:
app.use(bodyparser.json()); //utilizes the body-parser package
You can now replace the above line with:
app.use(express.json()); //Used to parse JSON bodies
This should not introduce any breaking changes into your applications since the code in express.json() is based on bodyparser.json().
If you also have the following code in your environment:
app.use(bodyParser.urlencoded({extended: true}));
You can replace the above line with:
app.use(express.urlencoded()); //Parse URL-encoded bodies
If you're getting a warning saying that you still need to pass extended to express.urlencoded() then, do update the above code as:
app.use(express.urlencoded({ extended: true }));
A final note of caution:
You might not need to install the additional body-parser package to your application if you are using Express 4.16+. There are many tutorials that include the installation of body-parser because they are dated prior to the release of Express 4.16.
In older versions of express, we had to use:
app.use(express.bodyparser());
because body-parser was a middleware between node and
express. Now we have to use it like:
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
body-parser is a piece of express middleware that
reads a form's input and stores it as a javascript
object accessible through req.body
'body-parser' must be installed (via npm install --save body-parser) For more info see: https://github.com/expressjs/body-parser
var bodyParser = require('body-parser');
app.use(bodyParser.json()); // support json encoded bodies
app.use(bodyParser.urlencoded({ extended: true })); // support encoded bodies
When extended is set to true, then deflated (compressed) bodies will be inflated; when extended is set to false, deflated bodies are rejected.
Instead of bodyParser.json(), simply use express.json(),
You don't want to install body-parser
For an instance,
const express = require("express");
const app = express();
app.use(express.json());
I found that while adding
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
helps, sometimes it's a matter of your querying that determines how express handles it.
For instance, it could be that your parameters are passed in the URL rather than in the body
In such a case, you need to capture both the body and url parameters and use whichever is available (with preference for the body parameters in the case below)
app.route('/echo')
.all((req,res)=>{
let pars = (Object.keys(req.body).length > 0)?req.body:req.query;
res.send(pars);
});
What is your opinion to use express-generator it will generate skeleton project to start with, without deprecated messages appeared in your log
run this command
npm install express-generator -g
Now, create new Express.js starter application by type this command in your Node projects folder.
express node-express-app
That command tell express to generate new Node.js application with the name node-express-app.
then Go to the newly created project directory, install npm packages and start the app using the command
cd node-express-app && npm install && npm start
body-parser deprecated bodyParser: use individual json/urlencoded middlewares node_modules\express\lib\router\layer.js:95:5
express deprecated req.host: Use req.hostname instead node_modules\body-parser\index.js:100:29
body-parser deprecated undefined extended: provide extended option node_modules\body-parser\index.js:105:29
No need to update express or body-parser
These errors will be removed. Follow these steps :-
app.use(bodyParser.urlencoded({extended: true})); // This will help in encoding.
app.use(bodyParser.json()); // this will support json format
It will run.
Happy Coding!
Check this answer
Stripe webhook error: No signatures found matching the expected signature for payload
// Use JSON parser for all non-webhook routes
app.use((req, res, next) => {
if (req.originalUrl === '/webhook') {
next();
} else {
express.json()(req, res, next);
}
});
// Stripe requires the raw body to construct the event
app.post('/webhook', express.raw({type: 'application/json'}), (req, res) => {
const sig = req.headers['stripe-signature'];
let event;
try {
event = stripe.webhooks.constructEvent(req.body, sig, webhookSecret);
} catch (err) {
// On error, log and return the error message
console.log(`❌ Error message: ${err.message}`);
return res.status(400).send(`Webhook Error: ${err.message}`);
}
// Successfully constructed event
console.log('✅ Success:', event.id);
// Return a response to acknowledge receipt of the event
res.json({received: true});
});
In my case it was typescript + vs code marking it wrongly as deprecated:
But if you check the source code:
/** #deprecated */
declare function bodyParser(
options?: bodyParser.OptionsJson & bodyParser.OptionsText & bodyParser.OptionsUrlencoded,
): NextHandleFunction;
declare namespace bodyParser {
you see it should be the constructor, not the namespace.
So either typescript or vs code is getting it wrong.
All good, there's no deprecation happening in this case (bodyParser as namespace=

Express.js req.body undefined

I have this as configuration of my Express server
app.use(app.router);
app.use(express.cookieParser());
app.use(express.session({ secret: "keyboard cat" }));
app.set('view engine', 'ejs');
app.set("view options", { layout: true });
//Handles post requests
app.use(express.bodyParser());
//Handles put requests
app.use(express.methodOverride());
But still when I ask for req.body.something in my routes I get some error pointing out that body is undefined. Here is an example of a route that uses req.body :
app.post('/admin', function(req, res){
console.log(req.body.name);
});
I read that this problem is caused by the lack of app.use(express.bodyParser()); but as you can see I call it before the routes.
Any clue?
UPDATE July 2020
express.bodyParser() is no longer bundled as part of express. You need to install it separately before loading:
npm i body-parser
// then in your app
var express = require('express')
var bodyParser = require('body-parser')
var app = express()
// create application/json parser
var jsonParser = bodyParser.json()
// create application/x-www-form-urlencoded parser
var urlencodedParser = bodyParser.urlencoded({ extended: false })
// POST /login gets urlencoded bodies
app.post('/login', urlencodedParser, function (req, res) {
res.send('welcome, ' + req.body.username)
})
// POST /api/users gets JSON bodies
app.post('/api/users', jsonParser, function (req, res) {
// create user in req.body
})
See here for further info
original follows
You must make sure that you define all configurations BEFORE defining routes. If you do so, you can continue to use express.bodyParser().
An example is as follows:
var express = require('express'),
app = express(),
port = parseInt(process.env.PORT, 10) || 8080;
app.configure(function(){
app.use(express.bodyParser());
});
app.listen(port);
app.post("/someRoute", function(req, res) {
console.log(req.body);
res.send({ status: 'SUCCESS' });
});
Latest versions of Express (4.x) has unbundled the middleware from the core framework. If you need body parser, you need to install it separately
npm install body-parser --save
and then do this in your code
var bodyParser = require('body-parser')
var app = express()
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }))
// parse application/json
app.use(bodyParser.json())
Express 4, has built-in body parser. No need to install separate body-parser. So below will work:
export const app = express();
app.use(express.json());
No. You need to use app.use(express.bodyParser()) before app.use(app.router). In fact, app.use(app.router) should be the last thing you call.
The Content-Type in request header is really important, especially when you post the data from curl or any other tools.
Make sure you're using some thing like application/x-www-form-urlencoded, application/json or others, it depends on your post data. Leave this field empty will confuse Express.
First make sure , you have installed npm module named 'body-parser' by calling :
npm install body-parser --save
Then make sure you have included following lines before calling routes
var express = require('express');
var bodyParser = require('body-parser');
var app = express();
app.use(bodyParser.json());
As already posted under one comment, I solved it using
app.use(require('connect').bodyParser());
instead of
app.use(express.bodyParser());
I still don't know why the simple express.bodyParser() is not working...
Add in your app.js
before the call of the Router
const app = express();
app.use(express.json());
The question is answered. But since it is quite generic and req.body undefined is a frequent error, especially for beginners, I find this is the best place to resume all that I know about the problem.
This error can be caused by the following reasons:
1. [SERVER side] [Quite often] Forget or misused parser middleware
You need to use appropriate middleware to parse the incoming requests. For example, express.json() parses request in JSON format, and express.urlencoded() parses request in urlencoded format.
const app = express();
app.use(express.urlencoded())
app.use(express.json())
You can see the full list in the express documentation page
If you can't find the right parser for your request in Express (XML, form-data...), you need to find another library for that. For example, to parse XML data, you can use this library
You should use the parser middleware before the route declaration part (I did a test to confirm this!). The middleware can be configured right after the initialization express app.
Like other answers pointed out, bodyParser is deprecated since express 4.16.0, you should use built-in middlewares like above.
2. [CLIENT side] [Rarely] Forget to send the data along with the request
Well, you need to send the data...
To verify whether the data has been sent with the request or not, open the Network tabs in the browser's devtools and search for your request.
It's rare but I saw some people trying to send data in the GET request, for GET request req.body is undefined.
3. [SERVER & CLIENT] [Quite often] Using different Content-Type
Server and client need to use the same Content-Type to understand each other. If you send requests using json format, you need to use json() middleware. If you send a request using urlencoded format, you need to use urlencoded()...
There is 1 tricky case when you try to upload a file using the form-data format. For that, you can use multer, a middleware for handling multipart/form-data.
What if you don't control the client part? I had a problem when coding the API for Instant payment notification (IPN). The general rule is to try to get information on the client part: communicate with the frontend team, go to the payment documentation page... You might need to add appropriate middleware based on the Content-Type decided by the client part.
Finally, a piece of advice for full-stack developers :)
When having a problem like this, try to use some API test software like Postman. The object is to eliminate all the noise in the client part, this will help you correctly identify the problem.
In Postman, once you have a correct result, you can use the code generation tool in the software to have corresponded code. The button </> is on the right bar. You have a lot of options in popular languages/libraries...
app.use(express.json());
It will help to solve the issue of req.body undefined
// Require body-parser (to receive post data from clients)
var bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({ extended: false }))
// parse application/json
app.use(bodyParser.json())
Looks like the body-parser is no longer shipped with express. We may have to install it separately.
var express = require('express')
var bodyParser = require('body-parser')
var app = express()
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }))
// parse application/json
app.use(bodyParser.json())
// parse application/vnd.api+json as json
app.use(bodyParser.json({ type: 'application/vnd.api+json' }))
app.use(function (req, res, next) {
console.log(req.body) // populated!
Refer to the git page https://github.com/expressjs/body-parser for more info and examples.
In case anyone runs into the same issue I was having; I am using a url prefix like
http://example.com/api/
which was setup with router
app.use('/api', router);
and then I had the following
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
What fixed my issue was placing the bodyparser configuration above app.use('/api', router);
Final
// setup bodyparser
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
//this is a fix for the prefix of example.com/api/ so we dont need to code the prefix in every route
app.use('/api', router);
Most of the time req.body is undefined due to missing JSON parser
const express = require('express');
app.use(express.json());
could be missing for the body-parser
const bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({extended: true}));
and sometimes it's undefined due to cros origin so add them
const cors = require('cors');
app.use(cors())
The middleware is always used as first.
//MIDDLEWARE
app.use(bodyParser.json());
app.use(cors());
app.use(cookieParser());
before the routes.
//MY ROUTES
app.use("/api", authRoutes);
express.bodyParser() needs to be told what type of content it is that it's parsing. Therefore, you need to make sure that when you're executing a POST request, that you're including the "Content-Type" header. Otherwise, bodyParser may not know what to do with the body of your POST request.
If you're using curl to execute a POST request containing some JSON object in the body, it would look something like this:
curl -X POST -H "Content-Type: application/json" -d #your_json_file http://localhost:xxxx/someRoute
If using another method, just be sure to set that header field using whatever convention is appropriate.
Use app.use(bodyparser.json()); before routing. // .
app.use("/api", routes);
History:
Earlier versions of Express used to have a lot of middleware bundled with it. bodyParser was one of the middleware that came with it. When Express 4.0 was released they decided to remove the bundled middleware from Express and make them separate packages instead. The syntax then changed from app.use(express.json()) to app.use(bodyParser.json()) after installing the bodyParser module.
bodyParser was added back to Express in release 4.16.0, because people wanted it bundled with Express like before. That means you don't have to use bodyParser.json() anymore if you are on the latest release. You can use express.json() instead.
The release history for 4.16.0 is here for those who are interested, and the pull request is here.
Okay, back to the point,
Implementation:
All you need to add is just add,
app.use(express.json());
app.use(express.urlencoded({ extended: true}));
app.use(app.router); // Route will be at the end of parser
And remove bodyParser (in newer version of express it is not needed)
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
And Express will take care of your request. :)
Full example will looks like,
const express = require('express')
const app = express()
app.use(express.json())
app.use(express.urlencoded({ extended: true}));
app.post('/test-url', (req, res) => {
console.log(req.body)
return res.send("went well")
})
app.listen(3000, () => {
console.log("running on port 3000")
})
You can try adding this line of code at the top, (after your require statements):
app.use(bodyParser.urlencoded({extended: true}));
As for the reasons as to why it works, check out the docs: https://www.npmjs.com/package/body-parser#bodyparserurlencodedoptions
Firsl of all, ensure you are applying this middleware (express.urlencoded) before routes.
let app = express();
//response as Json
app.use(express.json());
//Parse x-www-form-urlencoded request into req.body
app.use(express.urlencoded({ extended: true }));
app.post('/test',(req,res)=>{
res.json(req.body);
});
The code express.urlencoded({extended:true}) only responds to x-www-form-urlencoded posts requests, so in your ajax/XMLHttpRequest/fetch, make sure you are sending the request.setRequestHeader('Content-type', 'application/x-www-form-urlencoded'); header.
Thats it !
in Express 4, it's really simple
const app = express()
const p = process.env.PORT || 8082
app.use(express.json())
This occured to me today. None of above solutions work for me. But a little googling helped me to solve this issue. I'm coding for wechat 3rd party server.
Things get slightly more complicated when your node.js application requires reading streaming POST data, such as a request from a REST client. In this case, the request's property "readable" will be set to true and the POST data must be read in chunks in order to collect all content.
http://www.primaryobjects.com/CMS/Article144
Wasted a lot of time:
Depending on Content-Type in your client request
the server should have different, one of the below app.use():
app.use(bodyParser.text({ type: 'text/html' }))
app.use(bodyParser.text({ type: 'text/xml' }))
app.use(bodyParser.raw({ type: 'application/vnd.custom-type' }))
app.use(bodyParser.json({ type: 'application/*+json' }))
Source: https://www.npmjs.com/package/body-parser#bodyparsertextoptions
Example:
For me,
On Client side, I had below header:
Content-Type: "text/xml"
So, on the server side, I used:
app.use(bodyParser.text({type: 'text/xml'}));
Then, req.body worked fine.
To work, you need to app.use(app.router) after app.use(express.bodyParser()), like that:
app.use(express.bodyParser())
.use(express.methodOverride())
.use(app.router);
var bodyParser = require('body-parser');
app.use(bodyParser.json());
This saved my day.
I solved it with:
app.post('/', bodyParser.json(), (req, res) => {//we have req.body JSON
});
In my case, it was because of using body-parser after including the routes.
The correct code should be
app.use(bodyParser.urlencoded({extended:true}));
app.use(methodOverride("_method"));
app.use(indexRoutes);
app.use(userRoutes);
app.use(adminRoutes);
As I get the same problem, although I know BodyParser is no longer used
and I already used the app.use(express.json())
the problem was {FOR ME}:
I was placing
app.use(express.json())
after
app.use('api/v1/example', example) => { concerns the route }
once I reorder those two lines;
1 - app.use(express.json())
2 - app.use('api/v1/example', example)
It worked perfectly
If you are using some external tool to make the request, make sure to add the header:
Content-Type: application/json
This is also one possibility: Make Sure that you should write this code before the route in your app.js(or index.js) file.
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());

Resources