body-parser

Parse numbers from query strings with bodyParser.urlencoded() in express.js

走远了吗. 提交于 2020-02-25 09:39:52
问题 In the front end, I use jQuery to send a GET request like this: $.get('/api', {foo:123, bar:'123'), callback); according to jQuery doc, the 2nd parameter is a plain object that will be converted into query string of the GET request. In my node express back end, I use body-parser like this: const bodyParser = require("body-parser"); app.use(bodyParser.urlencoded({ extended: true })); app.get('/api', (req, res) => { console.log(req.query) // req.query should be the {foo:123, bar:'123'} I sent }

Node.js Express.js bodyParser POST limit

荒凉一梦 提交于 2020-02-01 04:22:04
问题 I'm trying to set the limit option for bodyParser.urlencodedParser as my POST data is bigger than the default value. My code currently looks like the following but whatever i try i always receive the following error: Error: request entity too large var express = require('express'); var router = express.Router(); var jsonfile = require('jsonfile'); var bodyParser = require('body-parser'); var urlencodedParser = bodyParser.urlencoded({limit: '5mb'}); router.post('/data', urlencodedParser

Increasing body limit size in apollo-server-express

若如初见. 提交于 2020-01-24 11:27:48
问题 I am trying to upload files to a webservice using the Upload scalar in apollo-server-express. I can upload small files(10s of kbs) to the webservice but am having trouble getting larger files to send. I did some searching and found that I can change the body size limit using the body-parser package, along with the bodyParserConfig option set in middleware. I've tried implementing this, as well as implementing it in the express app itself, however neither seem to be working. bodyParserConfig

Express body-parser handling checkbox arrays on forms

こ雲淡風輕ζ 提交于 2020-01-21 12:19:10
问题 I have an HTML form with an array of checkboxes (using [] naming). I need to be able to process this with express . I'm using body-parser . The problem is that unchecked checkboxes don't submit a value, and at the same time, body-parser seems to remove "holes" in arrays by simply packing the values into an array in order of indices, but ignoring the indices themselves. (Update: Actually it looks like qs is the culprit). Consider this full example, which displays a form and responds with a

Parse Error response in Express NodeJS app

泄露秘密 提交于 2020-01-06 08:45:08
问题 I am trying to send a specific error message to my front end. Scenario 1 app.use(function(err, req, res, next){ console.log(err); }); logs: [String: 'Error: Request returned error code: 422 and body: {"status":422,"title":"Missing or incorrect required fields","type":"https://developer.bigcommerce.com/api#api-status-codes","errors":{"variant":"This product has options, variant ID is required"}}'] Scenario 2 app.use(function(err, req, res, next){ res.status(422).send('TEST TEST TEST'); });

How to access raw body of a post request in Express.js?

你离开我真会死。 提交于 2020-01-06 07:16:31
问题 I send this request with postman. And I console.log(req.body) returns an array like this: { '{"urls":["https://example.com?paramOne': 'foo', paramTwo: 'bar"]}' } How can I get the whole body as a simple string like this? {"urls":["https://example.com?paramOne=foo&paramTwo=bar"]} 回答1: In app.js : Replace: app.use(express.json()); With: var rawBodySaver = function (req, res, buf, encoding) { if (buf && buf.length) { req.rawBody = buf.toString(encoding || 'utf8'); } } app.use(bodyParser.json({

Express' render/redirect does not work if the call isn't coming from a submit method in a form

自作多情 提交于 2019-12-30 10:08:12
问题 Task Perform a POST request from a JS method, so that variable values can be sent as parameters. Environment NodeJS Express BodyParser ejs My first attempt Frontend: <html> <head> <script src='http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js'></script> <script type="text/javascript"> function postAnswer() { $.post('vote', { message: "hello!"}, function(returnedData) { console.log("Post returned data: " + returnedData); }); } </script> </head> <body> <button id='send' onClick=

What does 'extended' mean in express 4.0?

穿精又带淫゛_ 提交于 2019-12-28 02:26:04
问题 I'm using express and also body-parser in my app. app.use(bodyParser.urlencoded({ extended: false })); But, What does 'extended' mean in express 4.0? I found this extended - parse extended syntax with the qs module. However, I still can't understrand what it means. 回答1: If extended is false , you can not post "nested object" person[name] = 'cw' // Nested Object = { person: { name: cw } } If extended is true , you can do whatever way that you like. 回答2: When extended property is set to true ,

Express, Multer, BodyParser req.body empty array

六月ゝ 毕业季﹏ 提交于 2019-12-25 07:38:54
问题 Sorry for such a noob question, but I have a form upload images and add some text to the DB, the images are uploading fine, but the req.body object is always an empty array. HTML <form class='new-project' action='/projects' method='POST' enctype="multipart/form-data"> <input type="text" name="title" placeholder="Project Title"> <br> <textarea name="description" rows="8" cols="40" placeholder="Project description"></textarea> <br> <label for='file'>Select your image:</label> <input type='file'

I can't get values from req.body node.js

*爱你&永不变心* 提交于 2019-12-25 03:36:25
问题 I need help with node.js: I have my req.body populated like this { '{ "email":"mail@hostname.com", "password":"12345" }' : '' } but I can't get values req.body.email and req.body.password are undefined My code is: user.js exports.loginByEmail= function(req, res) { console.log('POST'); console.log(req.body);//show values console.log(req.body.email);//undefined console.log(req.body.password);//undefined User.find({email:req.body.email,password:req.body.password}).toArray(function(err, userLoged