app.post和app.use在节点expression有什么不同?
我使用命令curl -H "Content-Type: application/json" -d '{"name":"sparc_core","port":["p1", "p2"]}' http://127.0.0.1:3000/add_module
来testingnodejs服务器。
起初,我的代码如下:
app.post('/add_module', bodyParser.json()); app.post('/add_module', bodyParser.urlencoded()); app.post('/add_module', function(req, res, next) { req.body = JSON.parse(req.body.data); next(); }); app.post('/add_module', function(req, res) { console.log("Start submitting"); console.log(req.body); ... ...
运行curl命令后,节点服务器输出错误信息如下:
SyntaxError:意外的令牌
在Object.parse(native)
在Object.app.post.res.send.error [as handle](/ home / xtec / Documents / xtec-simict / sim / app.js:80:21)
在next_layer(/ home / xtec / Documents / xtec-simict / sim / node_modules / express / lib / router / route.js:103:13)
在Route.dispatch(/home/xtec/Documents/xtec-simict/sim/node_modules/express/lib/router/route.js:107:5)
在/ home / xtec / Documents / xtec-simict / sim / node_modules / express / lib / router / index.js:205:24
在Function.proto.process_params(/home/xtec/Documents/xtec-simict/sim/node_modules/express/lib/router/index.js:269:12)
在下一个(/home/xtec/Documents/xtec-simict/sim/node_modules/express/lib/router/index.js:199:19)
在next_layer(/home/xtec/Documents/xtec-simict/sim/node_modules/express/lib/router/route.js:77:14)
在Object.urlencodedParser [as handle](/home/xtec/Documents/xtec-simict/sim/node_modules/body-parser/index.js:67:27)
在next_layer(/home/xtec/Documents/xtec-simict/sim/node_modules/express/lib/router/route.js:103:13)
POST / add_module 500 7ms – 1021b
那么我修改代码如下:
app.use(bodyParser.json()); app.use(bodyParser.urlencoded({ extended: true })); app.post('/add_module', function(req, res) { console.log("Start submitting"); console.log(req.body); ... ...
我运行相同的curl命令,它工作正常!
所以我想知道app.use和app.post的不同之处。 需要你的帮助,非常感谢你。
app.use()用于包含中间件/拦截器函数,当api被调用时,在执行实际函数之前执行该函数。
欲了解更多详情,请参阅 – 快递官方网站
例如 :
app.use(cors()); app.post("/",function(req,res){ });
上面的代码行相当于
app.post("/",cors(),function(req,res){ });
app.post,app.get,app.put,app.delete定义api的http方法。
请参考http://www.tutorialspoint.com/http/http_methods.htm链接http方法的更多细节
在你的情况
app.use(bodyParser.json()); app.use(bodyParser.urlencoded({ extended: true })); app.post('/add_module', function(req, res) { console.log("Start submitting"); console.log(req.body); }
当调用/ add_module api时,首先调用bodyParser.json()然后调用bodyParser.urlencoded({extended:true})函数
function(req, res) { console.log("Start submitting"); console.log(req.body);}
叫做 。 bodyParser.json()和bodyParse.urlencoded({extended:true})需要被调用的函数(req,res)
这是完全一样的,但是:
-
包含在app.use中的中间件将被用于所有的请求
-
包含在app.post中的中间件(“/ route”将仅用于匹配path/路由的POSTtypes的请求
例如,如果你的服务器包含以下内容:
// Common middleware app.use(function(req, res, next){ console.log("Middleware1"); return next(); }); app.use(function(req, res, next){ console.log("Middleware2"); return next(); }); // POST middleware app.post("/api/test1", function(req, res, next){ console.log("Middleware3"); return next(); }) app.post("/api/test2", function(req, res, next){ console.log("Middleware4"); return next(); }) // Actions app.post("/api/test1", function(req, res, next){ console.log("finalPOSTAction1"); return res.status(200).json({}); }) app.post("/api/test2", function(req, res, next){ console.log("finalPOSTAction2"); return res.status(200).json({}); }) app.get("/api/test3", function(req, res, next){ console.log("finalGETAction3"); return res.status(200).json({}); })
GET / api / test3上的GET请求会引发以下问题:
- Middleware1 - Middleware2 - finalGETAction3
POST / api / test1上的请求将引发以下操作:
- Middleware1 - Middleware2 - Middleware3 - finalPOSTAction1
并且/ api / test2上的POST请求会引发以下问题:
- Middleware1 - Middleware2 - Middleware4 - finalPOSTAction2