Node.js APIdevise和路由处理

我不知道该怎么称呼这个,但是我是Node.js新手。 我只是在GitHub上find了一个整洁的REST API项目来实现,但我不知道如何将所有的GET和POST等分割成单独的文件。

我有一个单独的api.js文件

 function API_ROUTER(router, connection, md5) { var self = this; self.handleRoutes(router, connection, md5); } API_ROUTER.prototype.handleRoutes = function(router, connection, md5) { router.get("/", function(req, res) { res.json({"Message" : "Hello World !"}); }); }; module.exports = API_ROUTER; 

现在我怎么能创build一个兄弟other.js和使用:

 var api = require('./api.js'); // Create router.get, router.post etc. here? 

但我不知道如何拆分所有GET和POST等分开的文件。

你可以组织你的路线的一种方法是为每个具有处理程序(由HTTP方法分隔)的路由以及其他所需的信息(如path)分别设置一个对象:

API / home.js

 module.exports = { path: '/', handlers: { 'get': function(req, res) { res.json({"Message" : "Hello World !"}); }, 'post': { // ... } // ... } } 

API / other.js

 module.exports = { path: '/other', handlers: { 'get': function(req, res) { res.json({"Message" : "Other !"}); }, // ... 

然后你可以在handleRoutes方法里加载所有这些:

 API_ROUTER.prototype.handleRoutes = function(router, connection, md5) { var routes = ['home', 'other']; routes.forEach(function(name) { // load the current route object (NOTE: you should use the path module for determining file path in a cross-platform manner) var routeObject = require('./' + name + '.js'); var apiPath = routeObject.path; var handlers = routeObject.handlers; var methods = Object.keys(handlers); // assign handlers for each method methods.forEach(function(method) { router[method](apiPath, handlers[method]); }); }); }; 

这将安装所有您的路线与适当的信息和处理程序。 现在你可以通过实例化API_ROUTER和必要的数据来调用这段代码:

 // initialize the api (and handle the routes internally) var Api = new require('./api.js')(router, connection, md5); 

如果你实现了一个RESTful API,那么你应该记住,这只是你提供数据的一种方式,你可能想在将来改变它,因为API大部分时间只是一个转换层。

通常情况下,您将根据资源分割代码,处理请求的代码不会有太多的逻辑,只会将请求传递给您的内部API。 为此,如果您已经使用express.js或类似的库,则不需要额外的图层。

在expressionapp.use([path,] function [, function...]) ,已经提供了模块化代码所需的function。 对于每个资源,您将创build一个自己的express.Router本身也可以挂载另一个子模块。 所以对于这部分你并不需要一个库。

什么时候图书馆可能有用:

  • 如果它自动将抛出的错误转换为正确的响应代码
  • 如果它包含一个工具来自动创build一个文档到您的API
  • 如果它完全抽象下层的路由系统,以便您可以挂钩到expresshapi ,…而不需要更改代码。

这里如何使用express.js进行设置

./lib/rest/customer.js

 var customerSystem = require('../customer-system'); var express = require('express'); var router = new express.Router(); router.get('/:id', function(req, res, next) { customerSystem.find({ id: req.params.id }, function(err, customer) { if (err) { res.status( /*correct status code*/ ).send( /*depending on the api return json, xml, ....*/ ) } else { res.send( /*depending on the api return json, xml, ....*/ ) } }) }); router.delete('/:id', function(req, res, next) { customerSystem.delete({ id: req.params.id }, function(err) { //... }); }); router.post('/', function(req, res, next) { //... }); //save the customer id for the pass to the sub routers router.use('/:id', function(req, res, next) { req.customerId = req.params.id; next(); }); router.use('/:id/addresses', require('./customer-address') ) module.exports = router; 

./lib/rest/customer-address.js

 var customerSystem = require('../customer-system'); var express = require('express'); var router = new express.Router(); router.get('/:id', function(req, res, next) { customerSystem.find({ id: req.customerId }, function(err, customer) { // ... }) }); /* ..... */ //save the address id for the pass to the sub routers router.use('/:id', function(req, res, next) { req.addressId = req.params.id; next(); }); router.use('/:id/addresses', require('./customer-address') ) module.exports = router;