【问题标题】:How do you test a expressApp.post()?你如何测试 expressApp.post()?
【发布时间】:2018-10-25 17:54:43
【问题描述】:

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

我的理解是需要转换如下

exports.dialogflowFirebaseFulfillment = functions.https.onRequest(app);

expressApp.post('/dialogflowFulfillment', (request, response) => { }

'use strict';
var debug = require('debug');
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');

var routes = require('./routes/index');
var users = require('./routes/users');

var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');

// uncomment after placing your favicon in /public
//app.use(favicon(__dirname + '/public/favicon.ico'));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

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

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

// error handlers

// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
    app.use(function (err, req, res, next) {
        res.status(err.status || 500);
        res.render('error', {
            message: err.message,
            error: 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: {}
    });
});

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

app.set('port', process.env.PORT || 3000);

var server = app.listen(app.get('port'), function () {
    debug('Express server listening on port ' + server.address().port);
});

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

我错过了什么?感谢您的帮助!

【问题讨论】:

  • 可以分享完整代码吗?
  • 我的错。我用代码更新了问题。

标签: node.js express dialogflow-es


【解决方案1】:

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

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 并转发到错误处理的函数按照here 的描述执行以下操作:

调用next()next(err) 表示当前处理程序是 完成,处于什么状态。 next(err) 将跳过所有剩余的 链中的处理程序,但设置为处理的处理程序除外 如上所述的错误。

【讨论】:

  • 非常感谢您为我解决了这个问题。我做出了改变,它奏效了。我对整个中间件处理 next() 事情仍然很模糊。我会检查你提到的参考资料。再次感谢。
【解决方案2】:

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

就您在app.use('/users', users); 中间件之后添加404 错误发送中间件而言,接下来将执行404 中间件 - 它会将错误(next(err);)发送到下一个中​​间件,该中间件捕获所有错误并发送回复。要解决此问题,您需要在所有其他中间件之后.use 错误处理中间件:

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。再次感谢。
猜你喜欢
  • 1970-01-01
  • 2010-10-07
  • 2016-01-19
  • 2010-09-25
  • 1970-01-01
  • 1970-01-01
  • 2021-10-14
  • 2010-09-23
  • 2013-04-19
相关资源
最近更新 更多