javascript 如何在特定路径前缀上挂载 app.get() 路由

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17173286/
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-27 07:22:06  来源:igfitidea点击:

How to mount app.get() routes on a particular path prefix

javascriptnode.jsexpressurl-routing

提问by Brian Beckett

I'm writing an API using Node.js and Express. My API has GET methods of the form:

我正在使用 Node.js 和 Express 编写 API。我的 API 具有以下形式的 GET 方法:

/api/v1/doSomething
/api/v1/doSomethingElse

My code is looking something like this:

我的代码看起来像这样:

server.js:

服务器.js:

var app = express();
...
var routes = require('./routes')
routes.attachHandlers(app, '/api/v1')

routes/index.js

路线/ index.js

...
module.exports.attachHandlers = function(app, context) {
    //get a list of all the other .js files in routes
    //for each route, require() it and call it myRoute
    myRoute.attachHandlers(app, context)
}

routes/some-route.js

路线/一些-route.js

...
module.exports.attachHandlers = function(app, context) {
    app.get(context + '/doSomething', doSomething)
    app.get(context + '/doSomethingElse', doSomethingElse)
}
...

Effectively I'm passing the context path/mount point down through the app. If somebody were to write a route like the following, though, the context would be lost:

实际上,我正在通过应用程序向下传递上下文路径/挂载点。但是,如果有人要编写如下所示的路由,上下文将丢失:

app.get('/doFoo', foo)

Rather than having that part of the API mounted on /api/v1/doFooit's on /doFoo. I would like to avoid having to pass the context path around like this.

而不是将 API 的那部分安装/api/v1/doFoo/doFoo. 我想避免像这样传递上下文路径。

app.usesupports mounting middleware on an optional mount path. I have seen references online to mounting an entire Express application on a mount path using app.use. This seems like the sort of thing I want to do, but I'm not sure how to do it or if it's the best solution for my particular use case.

app.use支持在可选的挂载路径上挂载中间件。我在网上看到过使用app.use. 这似乎是我想做的事情,但我不确定如何去做,或者它是否是我特定用例的最佳解决方案。

To summarise - I want to mount my app.get() routes with a particular prefix by default. What's the best way of doing this?

总而言之 - 我想默认使用特定前缀挂载我的 app.get() 路由。这样做的最佳方法是什么?

采纳答案by Peter Lyons

I think express-namespacewill work for this.

我认为express-namespace可以解决这个问题。

回答by marni

With Express 4.0, the task is much cleaner with the Router. You can create as many routers as you need to nicely partition your app, and then attached them with app.use(). For example:

使用 Express 4.0,路由器的任务更加清晰。您可以根据需要创建任意数量的路由器来很好地分区您的应用程序,然后使用 app.use() 附加它们。例如:

myapp.js

我的应用程序.js

var express = require("express"),
    router  = express.Router(),
    app     = express(),
    port    = 4000;


// Here we declare our API which will be visible under prefix path
router.get('/', function (req, res) {
    console.log("request to subspace hello");
    res.send({ message: "Hi from subspace /api/v1/"});
});

// we attach our routes under /api/v1
app.use('/api/v1', router);


// here we have direct, root-level routing
app.get('/', function (req, res) {
    console.log("request to rootspace hello");
    res.send({message: "Hi from root /"});
});

app.listen(port);
console.log("App active on localhost:" + port);

Then run

然后运行

node myapp.js

and visit

并参观

http://localhost:4000 and http://localhost:4000/api/v1

回答by JohnSz

Here's a working example of mounting a route in Express 3:

这是在 Express 3 中安装路由的工作示例:

./snipe3app.js

./snipe3app.js

var express = require('express');
var app = module.exports = express();

app.get('/subapp', function (req, res) {
  res.send('You are on the /sub/subapp page.');
});

./app.js

./app.js

var express = require('express'),
    http = require('http'),
    subApp = require('./snipe3app'),
    app = express();

app.use(express.favicon());
app.use(express.bodyParser());
app.use(app.router);
app.use('/sub', subApp);

app.get('/', function (req, res) {
  res.send('You are on the root page');
});

http.createServer(app).listen(3000, function(){
  console.log('Express server listening on port 3000. Point browser to route /secure');
});

You have to pay attention to the order in which the routes are handled when doing this.

执行此操作时,您必须注意处理路由的顺序。