Node.js 如何测试expressApp.post()?

Node.js 如何测试expressApp.post()?,node.js,express,dialogflow-es,Node.js,Express,Dialogflow Es,我是一名C#开发人员,正在尝试动态学习node.js/Dialogflow。我正试图在Azure上的node.js中创建一个webhook,作为Dialogflow项目的实现 我的理解是,我需要转换以下内容 exports.dialogflowFirebaseFulfillment=functions.https.onRequest(应用程序) 到 expressApp.post(“/dialogflowFulfillment”,(请求、响应)=>{ } 我的问题是如何测试“expressApp

我是一名C#开发人员,正在尝试动态学习node.js/Dialogflow。我正试图在Azure上的node.js中创建一个webhook,作为Dialogflow项目的实现

我的理解是,我需要转换以下内容

exports.dialogflowFirebaseFulfillment=functions.https.onRequest(应用程序)

expressApp.post(“/dialogflowFulfillment”,(请求、响应)=>{ }

我的问题是如何测试“expressApp.post()”以便知道它被调用。我使用Visual Studio 2017创建了一个空白的node.js express应用程序,然后添加了post函数。我运行应用程序,然后使用Postman应用程序将post请求发送到url(localhost:3000)/dialogflowFulfillment,但出现404错误


我缺少什么?感谢您的帮助!

您必须将您的函数移到错误处理之上:

app.use('/', routes);
app.use('/users', users);

// Put your function right here
app.post('/dialogflowFulfillment', function (req, res) {
  res.send('POST request to homepage');
});

// Catch 404 and forward to error handler
app.use(function (req, res, next) {
    var err = new Error('Not Found');
    err.status = 404;
    next(err);
});
捕获
404
并转发到错误处理的功能执行以下操作:

next()
next(err)
的调用表明当前处理程序正在运行 完成并处于什么状态。
下一步(错误)
将跳过所有剩余的操作 链中的处理程序,设置为处理的处理程序除外 如上所述的错误。


您必须将函数移到错误处理之上:

app.use('/', routes);
app.use('/users', users);

// Put your function right here
app.post('/dialogflowFulfillment', function (req, res) {
  res.send('POST request to homepage');
});

// Catch 404 and forward to error handler
app.use(function (req, res, next) {
    var err = new Error('Not Found');
    err.status = 404;
    next(err);
});
捕获
404
并转发到错误处理的功能执行以下操作:

next()
next(err)
的调用表明当前处理程序正在运行 完成并处于什么状态。
下一步(错误)
将跳过所有剩余的操作 链中的处理程序,设置为处理的处理程序除外 如上所述的错误。


Express发送
404
HTTP错误,因为添加中间件的顺序很重要

如果您在
app.use('/users',users);
中间件之后添加了
404
错误发送中间件,那么接下来将执行
404
中间件-它将抛出发送错误(
next(err);
)到下一个中间件,该中间件捕获所有错误并发送响应。要解决此问题,您需要
。在所有其他中间件之后使用
错误处理中间件:

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

// catch 404 and forward to error handler
app.use(function (req, res, next) {
    var err = new Error('Not Found');
    err.status = 404;
    next(err);
});

// production error handler
// no stacktraces leaked to user
app.use(function (err, req, res, next) {
    res.status(err.status || 500);
    res.render('error', {
        message: err.message,
        error: {}
    });
});

Express发送
404
HTTP错误,因为添加中间件的顺序很重要

如果您在
app.use('/users',users);
中间件之后添加了
404
错误发送中间件,那么接下来将执行
404
中间件-它将抛出发送错误(
next(err);
)到下一个中间件,该中间件捕获所有错误并发送响应。要解决此问题,您需要
。在所有其他中间件之后使用
错误处理中间件:

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

// catch 404 and forward to error handler
app.use(function (req, res, next) {
    var err = new Error('Not Found');
    err.status = 404;
    next(err);
});

// production error handler
// no stacktraces leaked to user
app.use(function (err, req, res, next) {
    res.status(err.status || 500);
    res.render('error', {
        message: err.message,
        error: {}
    });
});

你能分享完整的代码吗?我的错。我用代码更新了这个问题。你能分享完整的代码吗?我的错。我用代码更新了这个问题。非常感谢你为我解决了这个问题。我做了更改并且成功了。我对整个中间件处理下一个()仍不清楚事情。我会查看你提到的参考资料。再次感谢。非常感谢你为我解决了这个问题。我做了更改,它成功了。我对整个中间件处理下一个()仍不清楚事情。我将查看您提到的参考资料。再次感谢。感谢您回答我的问题。我不清楚您关于中间件路由的说法。我将学习Express。再次感谢。感谢您回答我的问题。我不清楚您关于中间件路由的说法。我将学习Expresss、 再次感谢。