简体   繁体   English

表达错误:抛出新的TypeError('Router.use()需要中间件功能,但得到了'+ gettype(fn));

[英]Express error: throw new TypeError('Router.use() requires middleware function but got a ' + gettype(fn));

I am a newbie to Express js. 我是Express js的新手。 I am following the tutorials from tutorials point 我正在从教程的角度关注教程

I tried implementing the examples given there but I keep getting the following error 我尝试实现此处给出的示例,但始终收到以下错误

/home/krishna/.nvm/versions/node/v5.0.0/bin/node /home/krishna/self/projects/projects/Node/nativebag/bin/www
/home/krishna/self/projects/projects/Node/nativebag/node_modules/express/lib/router/index.js:458
      throw new TypeError('Router.use() requires middleware function but got a ' + gettype(fn));
      ^

TypeError: Router.use() requires middleware function but got a Object
    at Function.use (/home/krishna/self/projects/projects/Node/nativebag/node_modules/express/lib/router/index.js:458:13)
    at EventEmitter.<anonymous> (/home/krishna/self/projects/projects/Node/nativebag/node_modules/express/lib/application.js:220:21)
    at Array.forEach (native)
    at EventEmitter.use (/home/krishna/self/projects/projects/Node/nativebag/node_modules/express/lib/application.js:217:7)
    at Object.<anonymous> 

(/home/krishna/self/projects/projects/Node/nativebag/app.js:25:5)
    at Module._compile (module.js:425:26)
    at Object.Module._extensions..js (module.js:432:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:311:12)
    at Module.require (module.js:366:17)

Process finished with exit code 1

I've looked for solutions online but wasn't able to solve it. 我已经在网上寻找解决方案,但无法解决。

Here is my code: 这是我的代码:

Index.js Index.js

var express = require('express');
var app = express();

var things = require('./things.js');

app.use('/things', things);
app.listen(3000);

things.js things.js

var express = require('express');
var router = express.Router();

/* GET home page. */
router.get('/', function(req, res, next) {
    res.render('index', { title: 'Express' });
});

// POST hello world
router.post('/', function (req, res, next) {
    res.send("Hello World");
});

module.exports = router;

Could some one point the error? 可以指出一点错误吗?

thanks. 谢谢。

It was essentially and issue of change in version of Express. 这本质上是Express版本更改的问题。 Below is the code that I changed and now it is working fine. 下面是我更改的代码,现在可以正常工作了。

var express = require('express');
var router = express.Router();

var things = require('./things.js');

router.use('/things', things);

module.exports = router;
// app.listen(3000);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Api : Express : throw new TypeError(&#39;Router.use() 需要一个中间件函数,但得到了一个 &#39; + gettype(fn)) - Api : Express : throw new TypeError('Router.use() requires a middleware function but got a ' + gettype(fn)) Express.js:抛出新的TypeError(&#39;Router.use()需要中间件功能,但得到了&#39;+ gettype(fn)) - Expressjs: throw new TypeError('Router.use() requires a middleware function but got a ' + gettype(fn)) throw new TypeError(&#39;Router.use() 需要中间件函数,但得到了一个 &#39; + gettype(fn)); - throw new TypeError('Router.use() requires middleware function but got a ' + gettype(fn)); throw new TypeError('Router.use() 需要一个中间件 function 但得到了一个 + gettype(fn))。 (其他问题) - throw new TypeError('Router.use() requires a middleware function but got a + gettype(fn)). (other problem) 护照.js +快递:TypeError(&#39;Router.use()需要中间件功能,但得到了&#39;+ gettype(fn)) - passport.js + express: TypeError('Router.use() requires a middleware function but got a ' + gettype(fn)) Node.js / express-Router.use()需要中间件功能,但是得到了&#39;+ gettype(fn)); - Nodejs/express - Router.use() requires middleware function but got a ' + gettype(fn)); TypeError(&#39;Router.use()需要中间件功能,但得到了&#39;+ gettype(fn))FeathersJS - TypeError('Router.use() requires a middleware function but got a ' + gettype(fn)) FeathersJS 我运行此代码,但显示 router.use() 需要中间件功能,但出现 &#39; + gettype(fn) 这个错误 - i run this code but showing router.use() requires a middleware function but got a ' + gettype(fn) this error 为什么 npm run 会导致错误:“Router.use() requires a middleware function but a &#39; + gettype(fn)”? - Why does npm run result in the error: “Router.use() requires a middleware function but a ' + gettype(fn)”? "Express 错误 - TypeError: Router.use() 需要中间件功能但得到了一个对象" - Express error - TypeError: Router.use() requires middleware function but got a Object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM