CNode

(回调函数)关于express中post方法的回调函数

问答
Lleon0516发布于11 年前最后回复11 年前3 回复4295 浏览0 收藏

app.post('/', function (req ,res) { res.send("hello"); });

以上代码中的匿名回调是如何运行的? 我没有找到这个post的方法,为什么执行匿名函数后req中为什么可以解析到请求?

ps:用到了body-parser中间件

小白一枚,求大神指点!感激不尽!

查看回复

回复 (3)

L
leapon#1·11 年前

app.post 在这里:

http://expressjs.com/api.html

app.METHOD(path, callback [, callback ...]) Routes an HTTP request, where METHOD is the HTTP method of the request, such as GET, PUT, POST, and so on, in lowercase. Thus, the actual methods are app.get(), app.post(), app.put(), and so on. See below for the complete list.

L
leapon#2·11 年前

请求的解析这里有讲:

http://expressjs.com/guide/routing.html

// respond with "hello world" when a GET request is made to the homepage
app.get('/', function(req, res) {
  res.send('hello world');
});
参与回复
登录后即可参与回复。登录