javascript Router.use 需要中间件功能吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30809485/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 12:43:18  来源:igfitidea点击:

Router.use requires middleware function?

javascriptnode.jsexpressmean-stack

提问by user3649245

So I'm trying to seperate my login routes in a seperate JS file called login_routes.js

所以我试图在一个名为 login_routes.js 的单独 JS 文件中分离我的登录路由

I keep getting this specific error:

我不断收到此特定错误:

TypeError: Router.use() requires middleware function but got a Object at Function. (/Users/ethanthomas/Desktop/mean-stuff/express-server/node_modules/express/lib/router/index.js:446:13)

类型错误:Router.use() 需要中间件函数,但在函数处得到了一个对象。(/Users/ethanthomas/Desktop/mean-stuff/express-server/node_modules/express/lib/router/index.js:446:13)

Not entirely understanding what it's asking me to implement?

不完全理解它要求我做implement什么?

login_routes.js:

login_routes.js:

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

app.route('/login')

.get(function(req, res, next) {
    res.send('this is the login form');
})

.post(function(req, res, next) {
    console.log('processing');
    res.send('proccessing the login form!');
});

server.js:

服务器.js:

var express = require('express');
var app = express();
var path = require('path');
var adminRoutes = require('./app/routes/admin_routes');
var loginRoutes = require('./app/routes/login_routes');

app.use('/admin', adminRoutes);
app.use('/login', loginRoutes);


//send our index.html file to the user for the home page
app.get('/', function(req, res) {
    res.sendFile(path.join(__dirname + '/index.html'));
});

//start the server
app.listen(1337);
console.log('leet is the magic port');

回答by Andreas

Your login_routes.jsshould look something like this (in the context of express):

您的login_routes.js应该看起来像这样(在 express 的上下文中):

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

// GET request to /login
router.get('/', function(req, res, next) {
    // do something
});

// POST request to /login
router.post('/', function(req, res, next) {
    // do something
});

module.exports = router;

In your app.jsyou use

在你的app.js你使用

var login_routes = require('./login_routes');
...
app.use('/login', login_routes);
...

Have a look at the code generated by the express-generatormodule. That is a starting point for express webserver apps.

查看express-generator模块生成的代码。这是快速网络服务器应用程序的起点。

回答by robertklep

People have already offered hints at the solution in comments.

人们已经在评论中提供了解决方案的提示。

The first issue is that you need to export your "sub" app from login_routes.js. To do so, change this:

第一个问题是您需要从login_routes.js. 为此,请更改以下内容:

var app = express();

Into this:

进入这个:

var app = module.exports = express();

Secondly, you are—probably unintentionally—creating routes for GET /login/loginand POST /login/login. To solve this, use this in login_routes.js:

其次,您可能是无意中为GET /login/login和创建路由POST /login/login。要解决此问题,请在login_routes.js以下内容中使用它:

app.route('/').get(...).post(...);

This is because the root path in your sub app (the one in login_routes.js) will get mapped to the path used in app.use()in your main app (server.js).

这是因为您的子应用程序中的根路径(login_routes.js)将映射到app.use()您的主应用程序 ( server.js) 中使用的路径。

回答by Jayesh

Do like this:

这样做:

login_routes.js:

login_routes.js:

exports.get = function( req, res ) {
  res.send('this is the login form');
};

exports.post = function( req, res ) {
  console.log('processing');
  res.send('proccessing the login form!');
};

server.js:

服务器.js:

var loginRoutes = require('./app/routes/login_routes');

app.get('/login', loginRoutes.get);
app.put('/login', loginRoutes.post);

回答by MEAbid

login_routes.js:

login_routes.js:

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

app.route('/login')

.get(function(req, res, next) {
    res.send('this is the login form');
})

.post(function(req, res, next) {
    console.log('processing');
    res.send('proccessing the login form!');
});

module.exports = router;

just writ module.exports = routerthen it will be work

只写module.exports = router然后它就会工作