Tag: body parser

Express.js + body-parser:POST请求中的空req.body

我正在使用Express和body-parser中间件来处理传入的请求。 在前端,我有一个表单,只是一个隐藏的input和一个提交button(写在帕格): form(notes="saveNotesForm" action=`/lessons/${lesson._id}/notes` method="POST" enctype="application/x-www-form-urlencoded") input(type="hidden" id="hiddenNotes" name="hiddenNotes" alt="Notes Copy" value="test").notesHidden input(type="submit" name="saveNotes" alt="Save Notes" value="Save") 在后端,我有Express应用程序使用body-parser: const bodyParser = require('body-parser'); // … app.use(bodyParser.json()); app.use(bodyParser.urlencoded({ extended: true })); 处理传入请求的路由: router.post('/lessons/:lessonID/notes', lessonController.updateNotes); // … and in lessonController: exports.updateNotes = async (req, res) => { console.log('updateNotes request received'); console.log(req.body); res.status(200).json({ status: 'success' }); } 当我尝试在req.body中使用req.body时,body是一个空对象,但是至less应该具有值为"test" hiddenNotes属性。 […]

允许string文字和JSON对象同时作为请求

我正在处理一个我希望接收的JSON对象(例如{data: "value1"} )和(在其他端点)一个string(例如"value1" )。 我目前正在使用快递中间件“body-parser”: const express = require("express"); const bodyParser = require("body-parser"); const server = express(); server.use(bodyParser.json()); …. some more code … 显然,这只支持json对象处理。 主要的疑问是,我可以支持接收除JSON对象以外的纯string吗? 如果是这样,那么最好的办法是什么? PD:这是我的第一个StackOverflow问题,所以请让我知道,如果我能以任何方式改善我的问题。 提前致谢..!

如何使用QuillJs和body-parser提交表单?

我是Nodejs的新手(通常是编码)。 我正在尝试构build一个小型的内容pipe理系统。 我希望我的潜在最终用户能够通过单击button将post提交到我的博客之前,对其内容进行样式设置(粗体,斜体,超级链接等)。 这种types的工具栏,我看到在Stackoverflow,因为我input这个 这是我阅读奎尔的指南后到目前为止: form.ejs <style> #editor { height: 300px; } </style> <!– Include Quill stylesheet –> <link href="https://cdn.quilljs.com/1.0.0/quill.snow.css" rel="stylesheet"> <!– Create the toolbar container –> <div id="toolbar"> <button class="ql-bold">Bold</button> <button class="ql-italic">Italic</button> </div> <form action = '/blog' method = 'POST'> <!– Create the editor container –> <input type = 'text' name = 'title' placeholder […]

NodeJs:使用Express比较URL参数和删除请求与数组值

所以,我有这些快递路线的对象数组,并做了一些CRUD操作,但我卡在“删除”之一。 我想通过抓取它的url参数ID来删除一个对象,并检查它是否与它的键的值匹配。 一切似乎工作,除了两件事情: “文本”键可以是任何东西(不关心,如果它不在数组中)。 只要匹配现有的url,它就不需要匹配它自己的“文本”。 我知道我错过了一些参数,但经过一段时间的search,尝试了几个不同的东西,我似乎无法把它放在手指上。 我是编程新手,可能说得不好,但请耐心等待。 app.js var foodList = [ { id:'e3ed4we23', text:'honey' }, { id:'12312oi3i2', text:'milk' }, { id:'1023u2kodw', text:'chicken' }, { id:'213021381203', text:'eggs' } ]; app.delete('/food-list/delete/:ingredientId', function(req, res) { var removeIngredient = req.body.text; if(!removeIngredient || removeIngredient.length == 0) { console.log('Cannot remove this ingredient:', removeIngredient); res.status(500).send({error:'Cannot remove invalid ingredient text'}); }else { […]

如何用NodeJS的bodyparser将Google的GWT请求体读入stringvariables?

我已经尝试了几乎所有可能的select从这个文档中的所有可能的方法: https : //github.com/expressjs/body-parser 我试图读取请求正文: app.use(bodyParser.text()); app.use(bodyParser.json()); app.use(bodyParser.raw()); app.use(bodyParser.urlencoded({ extended: false })); 我也试过一些选项,但每个版本都返回一个Object(我不能转换为String),而不是String。 是否有可能以某种方式读取这种请求types的正文? Content-Type: text/x-gwt-rpc; charset=UTF-8 谢谢你的帮助!

对象函数createServer()没有方法'bodyparser'

我是nodeJS的新手。 我正尝试使用连接中间件的不同中间件。 这是我的代码: var connect = require('connect'); var bodyParser = require('body-parser'); var cookieParser = require('cookie-parser'); var app = connect() .use(connect.bodyParser()) .use(connect.cookieParser('tobi is a cool ferret')) .use(function(req, res){ console.log(req.cookies); console.log(req.signedCookies); res.end('hello\n'); }).listen(3000); 我已经通过npm安装了每个中间件。 运行此文件时出现此错误。 /home/dipesh/Desktop/temp/temp.js:5 .use(connect.bodyParser()) ^ TypeError: Object function createServer() { function app(req, res, next){ app.handle(req, res, next); } merge(app, proto); merge(app, EventEmitter.prototype); app.route = […]

骨干model.save()不使用body parser设置express服务器的响应主体

我正在尝试使用model.save()保存(后)一个Backbone模型。 模型: Backbone.Model.extend({ urlRoot: '/project/', defaults: { projectname: "default projectname" } }); 保存模型(在我的Backbone.router对象中): putTest: function(id) { var projectItem = new ProjectModel({projectname: "This shiny new project"}); projectItem.save(); }, 我希望服务器上使用中间件“body-parser”的服务器上的节点/快速路由器可以在express request.body对象上获取模型的属性,如下所示: post: function(request, response) { console.log(request.body.projectname); } 但是response.body对象是空的。 当我使用method =“post”的常规html表单时,就像这样: <form id = "createProject" action = "/project/" method = "post" > <input type="text" name="projectName" value="Project name" /> […]

通过https JSON节点快递提交没有格式化

我有一个angularjs应用程序通过https发布到节点快递服务器。 angular度客户端使用标题发布: headers: {'Content-Type': 'application/x-www-form-urlencoded'}}) 在服务器上: var bodyParser = require('body-parser'); app.use(bodyParser.urlencoded({ extended: false })); 这产生了一个如下的请求体: { '{"firstname":"hj","lastname":"hj"}': '' } 没有bodyParser,我根本没有任何身体。 显然我做错了什么。 有没有办法得到有效的json而不尝试解读上述请求体?

为什么没有新版本的节点从请求体中删除__proto__?

当我发送PUT请求到我的快递服务器, req.body有时有一个__proto__属性,有时不是。 使用节点0.10.26和expression式3.2.3 : 当我把{"a":"b", "__proto__": {}} , 那么req.body是{"a":"b"} 使用节点4.1.0和expression式3.2.3 : 当我把{"a":"b", "__proto__": {}} , 然后req.body是{"a":"b", "__proto__": {}} req.body {"a":"b", "__proto__": {}} 所以更新版本的节点不会__proto__属性。 我其实很喜欢这种行为。 现在我必须写我自己的中间件去掉财产。 我认为这与bodyparser 。 奇怪的是,虽然,两个testing都有相同版本的express (以及相同版本的bodyparser )。 任何人都可以给出任何动机,为什么这是改变? build议如何解决这个问题?

使用angular.js和node.js将集合中的集合插入另一个服务器

这是我的控制器代码,我想把它发送到服务器,在那里服务器将数据存储在集合中。 $scope.addIdeas = function(){ console.log($scope.idea); var post_data = null; post_data = $scope.idea; console.log(post_data); $http({ url: 'http://127.0.0.1:3300/myroute/', method: 'post', }).then(function(response) { console.log(res); post_data = $scope.idea; console.log('from the function add' + post_data); }); }; 然后,这是我的服务器代码,我收到请求将其放入集合中。 //Adding the ideas to database app.post('/addIdeas', function(req, res){ console.log('i get req from idea controller'); console.log(req.body); db.ideas.insert(req.body, function (err, docs){ console.log(docs); res.json(docs); }); […]