【问题标题】:express.js app.post middleware not firingexpress.js app.post 中间件未触发
【发布时间】:2016-06-24 07:55:28
【问题描述】:

所以这会记录到我的控制台: app.use(function (req, res, next) { console.log(req.method) console.log('为什么不工作?') })

但这不是:

app.post(function (req, res, next) {
  console.log(req.method)
  console.log('why not working?')
})

两者都证明 HTTP 方法是一个 POST

我错过了什么?

【问题讨论】:

  • 你怎么称呼它?显示代码。
  • 而且,如果这应该是中间件,您还需要调用 next() 或发送响应。

标签: javascript node.js express


【解决方案1】:

app.post 期望 path 作为第一个参数。

查看更多信息http://expressjs.com/en/4x/api.html#app.post.method

例如:

app.post('/', function (req, res) {
  res.send('POST request to homepage');
});

app.use path - 可选参数,但在app.post 中是必需的。

【讨论】:

  • 啊,天才! : ) 那么,我该如何请求发布请求?
  • 检查req.method? :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-15
  • 1970-01-01
  • 2021-09-23
相关资源
最近更新 更多