Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js-外部中间件404_Node.js_Express_Middleware - Fatal编程技术网

Node.js-外部中间件404

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

我有一个简单的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 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');
}