[Express] Level 4: Body-parser -- Post
Parser Setup
Assume the body-parser
middleware is installed. Now, let's use it in our Express application.
npm install body-parser
Require the body-parser
npm module and assign it to a variable calledbodyParser
.
var bodyParser = require('body-parser');
The body-parser
middleware offers different parsing options. On thebodyParser
object, call a function that returns a parser for URL encoded data and store it in a variable called parseUrlencoded
. Remember to pass in an option which forces the use of the native querystring
Node library.
var parseUrlencoded = bodyParser.urlencoded({extended: false});
extended
- parse extended syntax with the qs module. (default:true
, but using the default has been deprecated. Please research into the difference betweenqs
andquerystring
and choose the appropriate setting)
For default qs model: https://www.npmjs.org/package/qs#readme
var obj = Qs.parse('a=c'); // { a: 'c' }
Read More: https://github.com/expressjs/body-parser
Mount the parser only in the post
route.
app.post('/cities',parseUrlencoded, function (request, response) { var city; });
Read the name
and description
parameters from the payload of the POSTrequest, and pass them as arguments to the createCity
function (we've created this one for you). Store the return value on the city
variable.
app.post('/cities',parseUrlencoded, function (request, response) { var city, name, description; city = request.body; name = city.name; description = city.description; createCity(name, description); });
Finally, respond back to the client with a 201 HTTP status code and the value stored in city
in JSON format using json
.
app.post('/cities',parseUrlencoded, function (request, response) { var city, name, description, cityName; city = request.body; name = city.name; description = city.description; cityName = createCity(name, description); response.status(201).json(cityName); //201: created });
var express = require('express'); var app = express(); var bodyParser = require('body-parser'); var parseUrlencoded = bodyParser.urlencoded({extended: false}); app.post('/cities',parseUrlencoded, function (request, response) { var city, name, description, cityName; city = request.body; name = city.name; description = city.description; cityName = createCity(name, description); response.status(201).json(cityName); //201: created }); app.listen(3000); var createCity = function(name, description){ cities[name] = description; return name; };
Validation
The way that it is now, we are allowing new cities to be created with a blank description. Let's add some validation so that in order for a city to be created, its description
must have a string length greater than 4.
Add an if
block that checks for a description.length
greater than 4, and move our city creation logic into that block. Use json()
to send the results from createCity
back to the client.
if(request.body.description.length > 4){ var city = createCity(request.body.name, request.body.description); response.status(201).json(city); }
If description
does not match its minimum length requirements, then set a400
status code (Bad Request) to the response, and set the response body toInvalid City
using json()
.
app.post('/cities', parseUrlencoded, function (request, response) { if(request.body.description.length > 4){ var city = createCity(request.body.name, request.body.description); response.status(201).json(city); }else{ response.status(400).json("Invalid City"); //400: bad request } });
var express = require('express'); var app = express(); var bodyParser = require('body-parser'); var parseUrlencoded = bodyParser.urlencoded({ extended: false }); app.post('/cities', parseUrlencoded, function (request, response) { if(request.body.description.length > 4){ var city = createCity(request.body.name, request.body.description); response.status(201).json(city); }else{ response.status(400).json("Invalid City"); //400: bad request } }); app.listen(3000);
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具