Extracting Express POST API data

By sean on Feb 24, 2014

Setting up a GET endpoint in Express is straight forward. However, creating a POST endpoint took a little research as the data posted to the endpoint isn't readily available without some intervention.

As of Express 4 body-parser is no longer included in express core.So we'll now need to include the body-parser middleware.

npm install body-parser --save
var express = require('express');
var bodyParser = require('body-parser');
var app = express();

app.use(bodyParser.urlencoded({extended: false}));
app.use(bodyParser.json());

app.post('/user/login', function(req, res) {
  console.log(req,body);
  console.log(req.body.username +' '+ req.body.password);
});

Comments

Sign in to comment.
Hawkee   -  Oct 04, 2014

How about some more node code? I was thinking about doing a page about Passport and Twitter/Facebook integration.

 Respond  
Hawkee   -  Feb 24, 2014

consoloe.log? Good to see some node.js code for a change!

sean  -  Feb 24, 2014

Thanks, fixed the typo. It would be great to collaborate with others on web related projects

Hawkee  -  Feb 25, 2014

You mean like a code wiki where others can edit or add to the snippet pages?

F*U*R*B*Y*  -  Feb 25, 2014

That wouldn't be a bad idea Hawkee!!!

Hawkee  -  Feb 25, 2014

It would have to be a branching system where a snippet is a branch of another. Somewhat like GitHub.

sean  -  Feb 27, 2014

Nope, I meant that I wished there were more web developers within the Hawkee community to bounce ideas off of etc. (Sorry, I should have chosen my terms more carefully). I'm not sure a branching system would catch on here.

Hawkee  -  Feb 27, 2014

I agree, so I'm cutting the fat to streamline the site for a wider audience. It's just become too cumbersome to work with over the years. I'm upgrading the site to Bootstrap 3 at the moment and since the Hardware section is gone, my workload has been cut in half.

F*U*R*B*Y*  -  Mar 03, 2014

When i get back into coding, Sean i'm going to be bouncing heaps of questions/ideas/thoughts/suggestions your way :)

user127  -  Jul 08, 2014

I have seen the suggestion to use the express methods before:

   app.use(express.urlencoded());
   app.use(express.json());

That results in an error saying that these are no longer part of express and must be installed separately. Any suggestions on how exactly to do that? All the help text I find seems to reference previous versions, installing connect, etc.

Thanks

Sign in to comment

Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.