0
var express = require('express');
var bodyParser = require('body-parser');
var app = express();

app.use(bodyParser.json());
//app.use(express.urlencoded());
app.use(bodyParser.urlencoded({// to support URL-encoded bodies
    extended: true`enter code here`
}));

app.post('/register', function (req, res) {
    console.log("inside log");
    console.log(req.body);
    var username = req.body.username;
    console.log(username);
});

As I have used urlencoded() whenever I post data in form data, I do not get data in req.body. i have attached image of postman

in case I send data in x-www-urlencoded then I get data

enter image description here

Please give me a solution to parse the data. I want to pass it in form data because I will be uploading an image too. So for image form data is must

1 Answer 1

1

You need use connect-multiparty npm module.

  global.app = module.exports = express();

var multipart = require('connect-multiparty');

app.use(multipart());

You not set content-type in postman because node server automatically set his content-type in header according to form-data file.

Sign up to request clarification or add additional context in comments.

2 Comments

Yes. This works fine . But then Image doesnot get uploaded. I am uploading the image using blueimp-file-upload-expressjs. But it given error _http_outgoing.js:335 throw new Error('Can\'t set headers after they are sent.');
this is callback error . Just check u have not done callback twice in code .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.