2

我想将自定义路由添加到使用express作为服务器的bottender 。但是当我这样做时,bottender(应该是)使用的路由返回404。这是它的样子/messenger

index.js

require('dotenv').config();

const { MessengerBot } = require('bottender');
const express = require('express');
const bodyParser = require('body-parser');
const { registerRoutes } = require('bottender/express');
const helmet = require('helmet');
const cors = require('cors');
const compression = require('compression');

const handler = require('./handlers');
const { messenger } = require('./config');
const logger = require('./utils/logger');

const errorController = require('./controllers/errorController');
const ticketsController = require('./controllers/ticketsController');

const { APP_PORT, NODE_ENV } = process.env;
const server = express();

/* istanbul ignore next */
const verify = (req, res, buf) => {
  req.rawBody = buf.toString();
};

server.use('/images', express.static('images'));
server.use('/facebook-payment', express.static('payment-page/build'));
server.use(compression());
server.use(helmet());
server.use(cors());
server.use(bodyParser.json({ verify }));
server.use(require('morgan')('short', { stream: logger.logStream }));

const router = express.Router();
/* Custom APIs */
server.use('/api', router);
server.use('/api/tickets', ticketsController.getByEventId);

/* Error Handling */
server.use(errorController.error404);
server.use(errorController.generalError);

const bots = {
  messenger: new MessengerBot(messenger.bot).onEvent(handler.messenger.execute),
  // Define other platform bots here!
};

registerRoutes(server, bots.messenger, messenger.server);
// Start server
server.listen(APP_PORT, () => logger.info(`ENV[${NODE_ENV}] - server is listening on port ${APP_PORT}...`));

module.exports = server;

配置.js

module.exports = {
  messenger: {
    profile,
    bot: {
      accessToken: FB_ACCESS_TOKEN,
      appId: APP_ID,
      appSecret: APP_SECRET,
      mapPageToAccessToken: bots.getTokenByPageById,
    },
    server: {
      verifyToken: FB_VERIFY_TOKEN,
      path: '/messenger',
    },
  },
};

关于为什么bottender使用的路由返回404的任何线索?

提前致谢!

4

2 回答 2

2

尽管我不确定您的 实现是什么errorController,但您可能想尝试将其放在registerRoutes()他们面前。

如:

// register before error handling
const bots = { /* your bots */}
registerRoutes(server, bots.messenger, messenger.server);

/* Error Handling */
server.use(errorController.error404);
server.use(errorController.generalError);

server.listen(/* ... */)
于 2018-03-02T13:21:12.573 回答
1

在注册自定义 API 之前,我能够通过使用第一个来解决这个问题registerRoutes 所以代码看起来像这样

const { registerRoutes } = require('bottender/express');

/* register bottender/express route */
registerRoutes(server, bots.messenger, messenger.server); // needs to be declared first before error handler
/* Error Handling */
app.use(errorController.error404);
app.use(errorController.generalError);
于 2018-03-02T13:21:08.427 回答