I'm trying to setup my express routing to have the following structure
src
|-app.js
|-api
|-v1
|-index.js
|-users
|-routes.js
|-controller.js
|-tests.js
|-currency
|-routes.js
|-controller.js
|-tests.js
The idea is to keep each part of the API contained together.
I want index.js to basically identify all routes.js files within the subfolders of the directory it belongs to and require them into the express router as required and then required in through the app.js as the appropriate version of the 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/currency/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;
I've tried debugging it with the DEBUG module and can see that express recognizes the routes.
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
However when I try to access http://localhost:3000/api/v1/currency I get the following {"message":"Not Found","error":{"status":404}}.
Anyone have any ideas/suggestions.
Thanks
Paul
via Paul Mowat
No comments:
Post a Comment