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

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

问题描述

我以 NodeJS Express 4 开始,我有点困惑。我一直在阅读 express 网站,但无法看到_when使用路由处理程序或何时使用 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)  

在开始的时候,我以为对于 express3 ,是对的,或者这是 express4 的方式?

At the beginning, I thought this was old, for express3, is that right or this is the way for express4 too?

如果这是在 express4 中执行的方式,什么是 express.Router 用于?

If this is the way to do it in express4, 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)

那么,两个例子?

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

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;

express()在应用程序中调用.js,返回一个app对象。将应用对象视为Express应用程序。

When express() is called in app.js, an app object is returned. Think of an app object as an Express application.

express.Router()被调用时,略有不同迷你应用返回。 迷你应用程序背后的想法是,应用程序中的不同路线可能变得相当复杂,您可以从将该逻辑转移到单独的文件中获益。

When express.Router() is called, a slightly different "mini app" is returned. The idea behind the "mini app" is that different routes in your app can become quite complicated, and you'd benefit from moving that logic into a separate file.

上面这个简单的例子, / dogs 路由的逻辑已经被移动到自己的文件中,所以它的GET和POST处理程序不会混乱app.js.现在,您可以单独处理对 / dogs 的任何请求的逻辑,而不用担心如何影响 / cats / birds

In this simple example above, the logic for the /dogs route has been moved into its own file just so its GET and POST handlers won't clutter up app.js. Now you can work on the logic for any requests to /dogs in isolation and not worry about how it will affect /cats and /birds.

如果您有与所有三条路线相关的逻辑(在Express中称为中间件),则可以将其放在应用程序上方的app.js中。使用(...)调用。如果您有逻辑只适用于其中一条路线( / dogs ),则将其放在该路由的文件中。

If you have logic (called middleware in Express) that pertains to all three routes, you can put it in app.js above the app.use(...) calls. If you have logic that pertains to just one of those routes (/dogs), then you put it in the file for that route only.

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

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