express.Router 和 app.get 的区别? [英] Differences between express.Router and app.get?

查看:28
本文介绍了express.Router 和 app.get 的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从 NodeJS 和 Express 4 开始,我有点困惑.我一直在阅读 Express 网站,但看不到 何时 使用路由处理程序或何时使用 express.Router.

I'm starting with NodeJS and Express 4, and I'm a bit confused. I been reading the Express website, but can't see when to use a route handler or when to use express.Router.

如我所见,如果我想在用户点击 /show 时显示页面或其他内容,我应该使用:

As I could see, if I want to show a page or something when the user hits /show for example I should use:

var express = require('express')    
var app = express()    
app.get("/show", someFunction)  

一开始,我认为这是旧的(对于 Express 3).是这样还是 Express 4 也是这样?

At the beginning, I thought this was old (for Express 3). Is that right or this is the way for Express 4 too?

如果这是 Express 4 中的做法,那么 express.Router 是做什么用的?

If this is the way to do it in Express 4, what is express.Router used for?

我阅读了与上面几乎相同的示例,但使用了 express.Router:

I read almost the same example as above but using express.Router:

var express = require('express');
var router = express.Router();
router.get("/show", someFunction)

那么,这两个示例之间有什么区别?

So, what's the difference between both examples?

如果我只想做一个简单的测试网站,我应该使用哪个?

Which one should I use if I just want to do a simple testing website?

推荐答案

app.js

var express = require('express'),
    dogs    = require('./routes/dogs'),
    cats    = require('./routes/cats'),
    birds   = require('./routes/birds');

var app = express();

app.use('/dogs',  dogs);
app.use('/cats',  cats);
app.use('/birds', birds);

app.listen(3000);

dogs.js

var express = require('express');

var router = express.Router();

router.get('/', function(req, res) {
    res.send('GET handler for /dogs route.');
});

router.post('/', function(req, res) {
    res.send('POST handler for /dogs route.');
});

module.exports = router;

var app = express() 被调用时,会返回一个 app 对象.将此视为主应用.

When var app = express() is called, an app object is returned. Think of this as the main app.

var router = express.Router() 被调用时,返回一个略有不同的迷你应用.迷你应用背后的理念是,您的应用中的每条路线都可能变得非常复杂,将所有代码移动到一个单独的文件中会让您受益匪浅.每个文件的路由器成为一个迷你应用,其结构与主应用非常相似.

When var router = express.Router() is called, a slightly different mini app is returned. The idea behind the mini app is that each route in your app can become quite complicated, and you'd benefit from moving all that code into a separate file. Each file's router becomes a mini app, which has a very similar structure to the main app.

在上面的示例中,/dogs 路由的代码已移动到其自己的文件中,因此它不会使主应用程序 变得混乱./cats/birds 的代码在它们自己的文件中的结构类似.通过将此代码分成三个迷你应用,您可以单独处理每个应用的逻辑,而不必担心它会如何影响其他两个.

In the example above, the code for the /dogs route has been moved into its own file so it doesn't clutter up the main app. The code for /cats and /birds would be structured similarly in their own files. By separating this code into three mini apps, you can work on the logic for each one in isolation, and not worry about how it will affect the other two.

如果您有与所有三个路由相关的代码(中间件),您可以将其放在主应用中,在 app.use(...) 之前调用.如果您的代码(中间件)仅与其中一个路由相关,则可以将其放入该路由的文件中.

If you have code (middleware) that pertains to all three routes, you can put it in the main app, before the app.use(...) calls. If you have code (middleware) that pertains to just one of those routes, you can put it in the file for that route only.

这篇关于express.Router 和 app.get 的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆