我正在尝试设置我的快速路由以具有以下结构
src
|-app.js
|-api
|-v1
|-index.js
|-users
|-routes.js
|-controller.js
|-tests.js
|-currency
|-routes.js
|-controller.js
|-tests.js
我们的想法是将API的每个部分都包含在一起。
我希望index.js基本上识别它所属目录的子文件夹中的所有routes.js文件,并根据需要将它们放入快速路由器中,然后通过app.js将其作为api的相应版本
app.js
app.use('/api/v1', require('./api/v1'));
API / V1 / index.js
/*jslint node: true */
"use strict";
var express = require('express');
var router = express.Router();
var fs = require('fs');
fs.readdirSync(__dirname).forEach(function(file) {
// Remove extension from file name
var basename = file.split('.')[0];
// Only load files that aren't directories and aren't blacklisted
if (fs.lstatSync(__dirname + '/' + file).isDirectory()) {
console.log("api: " + '/' + basename + ' file: ./' + file + '/routes.js');
router.use('/' + basename, require('./' + file + '/routes.js'));
}
});
module.exports = router;
API / V1 /货币/ routes.js
/*jslint node: true */
"use strict";
// *******************
// Currency Routes
// *******************
var express = require('express');
var router = express.Router();
router.get('/currency', function (req, res) {
res.send('Currency List Page');
});
router.get('/currency/:id', function (req, res) {
res.send('Currency Page: ' + req.params.id);
});
module.exports = router;
我已经尝试使用DEBUG模块进行调试,并且可以看到express识别路由。
C:\DevelopmentOE\Node\src>set DEBUG=express:* & node ./bin/www.js
express:application set "x-powered-by" to true +0ms
express:application set "etag" to 'weak' +3ms
express:application set "etag fn" to [Function: wetag] +2ms
express:application set "env" to 'development' +0ms
express:application set "query parser" to 'extended' +0ms
express:application set "query parser fn" to [Function: parseExtendedQueryString] +1ms
express:application set "subdomain offset" to 2 +0ms
express:application set "trust proxy" to false +0ms
express:application set "trust proxy fn" to [Function: trustNone] +1ms
express:application booting in development mode +0ms
express:application set "jsonp callback name" to 'callback' +0ms
express:router use '/' query +16ms
express:router:layer new '/' +0ms
express:router use '/' expressInit +1ms
express:router:layer new '/' +0ms
express:router use '/' jsonParser +0ms
express:router:layer new '/' +0ms
express:router use '/' urlencodedParser +4ms
express:router:layer new '/' +1ms
api: /currency file: ./currency/routes.js
express:router:route new '/currency' +5ms
express:router:layer new '/currency' +1ms
express:router:route get '/currency' +0ms
express:router:layer new '/' +1ms
express:router:route new '/currency/:id' +0ms
express:router:layer new '/currency/:id' +1ms
express:router:route get '/currency/:id' +0ms
express:router:layer new '/' +0ms
express:router use '/currency' router +1ms
express:router:layer new '/currency' +0ms
api: /users file: ./users/routes.js
express:router:route new '/user' +3ms
express:router:layer new '/user' +0ms
express:router:route get '/user' +1ms
express:router:layer new '/' +0ms
express:router:route new '/user/:id' +0ms
express:router:layer new '/user/:id' +0ms
express:router:route get '/user/:id' +1ms
express:router:layer new '/' +0ms
express:router use '/users' router +0ms
express:router:layer new '/users' +1ms
express:router use '/info' <anonymous> +0ms
express:router:layer new '/info' +1ms
express:router use '/api/v1' router +0ms
express:router:layer new '/api/v1' +0ms
express:router use '/' <anonymous> +1ms
express:router:layer new '/' +0ms
express:router use '/' <anonymous> +0ms
express:router:layer new '/' +1ms
express:application set "port" to 3000 +3ms
但是,当我尝试访问http://localhost:3000/api/v1/currency时,我收到以下{“消息”:“未找到”,“错误”:{“状态”:404}}。
答案 0 :(得分:1)
从我所看到的,你是在定义/api/v1/currency/currency
api/v1/currency/routes.js
应该定义这些路线:
router.get('/', ...);
router.get('/:id', ...);