body-parser

fetch POST fails on post json body with sails.js

大城市里の小女人 提交于 2019-12-11 06:48:40
问题 I'm getting the following error within the body-parser when I try to POST a simple json body in to my controller using fetch 1] error: Unable to parse HTTP body- error occurred :: 'SyntaxError: `Unexpected token -\n at parse (/project/node_modules/body-parser/lib/types/json.js:83:15)\n at /project/node_modules/body-parser/lib/read.js:116:18\n at invokeCallback (/project/node_modules/raw-body/index.js:262:16)\n at done (/project/node_modules/raw-body/index.js:251:7)\n at IncomingMessage.onEnd

Express: Req.body is undefined after POST req

♀尐吖头ヾ 提交于 2019-12-11 04:06:10
问题 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); })

Why is request.body undefined?

依然范特西╮ 提交于 2019-12-11 03:08:48
问题 I have a node-js server which includes bodyparser and everything: var express = require('express'); var dbcon = require('./app/db/databaseconnection'); var bodyParser = require('body-parser'); var app = express(); var router = express.Router(); var filepath = __dirname + '/views/'; app.set('view engine', 'ejs'); app.use(express.static(__dirname + '/public')); app.use('/', router); app.use(bodyParser); app.use(bodyParser.json()); // support json encoded bodies app.use(bodyParser.urlencoded({

Posting form data with Nodejs and body-parser

拟墨画扇 提交于 2019-12-10 10:24:28
问题 I have followed a couple of different online attempts at this now and I keep getting undefined for my post data and console.log(JSON.stringify(req.body)) returns nothing also.. So I am going wrong somewhere... HTML: <!DOCTYPE HTML> <html> <head> <title>Chat</title> </head> <body> <form action="/" method="post"> <button>Close</button><br/><br/> <label for="username">Your Name: *</label><br/> <input id="username" type="text" value="" name="username" autocomplete="off" required="required" /><br/

Parse body from GET request using nodejs, express, body-parser?

旧时模样 提交于 2019-12-08 03:27:56
问题 Is it possible to retrieve the body contents using express ? I started by trying body-parser but that doesn't seem to work with GET . Are there any modules which would work? var express = require('express'), bodyParser = require('body-parser'), PORT = process.env.PORT || 4101, app = express(); app.use(bodyParser.urlencoded({ extended: false })); app.use(bodyParser.json()); app.route('/') .get(function(req, res) { respond(req, res, 'GET body contents:\n'); }) .post(function(req, res) { respond

Sails.js :remove bodyparser middleware for a specific route

好久不见. 提交于 2019-12-07 09:59:34
Is there any way to remove middleware for a particular route currently all the middlewares are listed in http.js file [ 'startRequestTimer', 'cookieParser', 'session', 'bodyParser', 'passportInit', 'passportSession', 'myRequestLogger', 'handleBodyParserError', 'compress', 'methodOverride', 'poweredBy', 'router', 'www', 'favicon', '404', '500' ] I want to remove the bodyParser middleware for a particular route Is it possible with sails.js?? There's no mechanism for doing this declaratively, but yes, you can disable a middleware on a per-route basis. You would do it by overriding the middleware

How to know which button was pressed?

不问归期 提交于 2019-12-07 01:56:52
问题 I'm learning the basics of node.js and express framework. I have a simple page with two buttons: <form action="/home2" method="post"> <button name="butt1">butt1</button> <button name="butt2">butt2</button> </form> And i want to see in console which button was pressed: router.post('/', function(req, res, next) { console.log(req.body.name); res.render('home2', { title: 'post' }); }); In the console i just see undefined How can I access the name of the button? 回答1: I think it would be helpful

Can not post the nested object json to node express body parser

ⅰ亾dé卋堺 提交于 2019-12-06 08:13:12
问题 Hi I'm creating sample REST api using Node, Express and Mongo. I'm using bodyParser() middle ware to parse the form data. Its working fine for simple object say var user = { name:'test', age:'20' } req.body produce the same set of format to save it in the mongodb like. { name:'test', age:'20' } When using complex object var user = { name:'test', age:'20', education: { institute:"xxx", year:2010 } } req.body produce different format something like { name:'test', age:'20', education[institute]:

Express body-parser req.body with formdata is empty object

﹥>﹥吖頭↗ 提交于 2019-12-05 15:01:21
Somehow my req.body is always empty, maybe you have an idea: here is my server code: const Express = require('express'); const bodyParser = require('body-parser'); const app = new Express(); app.use(bodyParser.urlencoded({ extended: true })); app.use(bodyParser.json()); app.post('/save', (req, res) => { console.log(req.body) // => {} res.send(req.body); }); const env = process.env.NODE_ENV || 'production'; app.listen(3000, err => { if (err) { return console.error(err); } console.info(`Server running on http://localhost:${port} [${env}]`); }); When I try to send formdata with javascript the req

How to know which button was pressed?

北慕城南 提交于 2019-12-05 08:02:47
I'm learning the basics of node.js and express framework. I have a simple page with two buttons: <form action="/home2" method="post"> <button name="butt1">butt1</button> <button name="butt2">butt2</button> </form> And i want to see in console which button was pressed: router.post('/', function(req, res, next) { console.log(req.body.name); res.render('home2', { title: 'post' }); }); In the console i just see undefined How can I access the name of the button? I think it would be helpful for you. <form action="/home2" method="post"> <button name="butt1">butt1</button> <button name="butt2">butt2<