20

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

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

My code is looking something like this:

server.js:

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

routes/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

...
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/doFoo it's on /doFoo. I would like to avoid having to pass the context path around like this.

app.use supports 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.

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

4

3 回答 3

43

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

myapp.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);

然后运行

node myapp.js

并访问

http://localhost:4000 and http://localhost:4000/api/v1
于 2014-05-22T21:10:11.983 回答
7

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

./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

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');
});

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

于 2013-06-18T19:43:04.223 回答
5

我认为express-namespace将为此工作。

于 2013-06-18T19:00:27.220 回答