Node.js-外部中间件404
我有一个简单的express应用程序(版本4.9.0),我正在尝试将我的中间件放入外部文件中 我的app.js有:Node.js-外部中间件404,node.js,express,middleware,Node.js,Express,Middleware,我有一个简单的express应用程序(版本4.9.0),我正在尝试将我的中间件放入外部文件中 我的app.js有: var中间件=require('./lib/middleware'); get('/foo',middleware.configcache) /lib/middleware包含index.js: exports.configcache=require('./configcache.js') /lib/middleware/configcache.js包含: function con
var中间件=require('./lib/middleware');
get('/foo',middleware.configcache)代码>
/lib/middleware包含index.js:
exports.configcache=require('./configcache.js')代码>
/lib/middleware/configcache.js包含:
function configcache(req, res, next) {
console.log("hello world");
next();
}
module.exports = configcache;
当我向/foo发出GET请求时,我得到一个404。有人能提供建议吗?这是我在一个应用程序中使用它的方式:
app.js:
routes/index.js:
我用:yoexpress
所以问题的答案是:不要调用next(),而是在使用router.get()时向浏览器发送实际响应:
或者使用路由器。请按此处所述使用:谢谢!确实很有帮助。
var routes = require('./routes/index');
var express = require('express');
var router = express.Router();
/* GET home page. */
router.get('/', function(req, res) {
res.render('index', {
title: req.i18n.t("meta.title.index")
});
});
module.exports = router;
function configcache(req, res, next) {
res.send(200, 'hello world');
}