Socket
Socket
Sign inDemoInstall

express-quick-routes

Package Overview
Dependencies
0
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 0.0.0 to 1.0.0

test-routes/index.js

123

index.js

@@ -1,33 +0,112 @@

exports.init = function(routesLocation){
var fs = require('fs')
, path = require('path')
, sugar = require('sugar')
, router = {}
, routeFolder = '';
if(routesLocation){
routeFolder = path.resolve(routesLocation);
} else {
routeFolder = path.resolve(path.dirname(process.mainModule.filename) + '/routes/');
}
'use strict';
fs.readdirSync(routeFolder).forEach(function(file){
var keyName = '';
if(file.last(3) == '.js'){
keyName = file.first(file.length-3);
} else if(file.last(7) == '.coffee'){
keyName = file.first(file.length-7);
/*!
express-quick-routes
Copyright(c) 2013 Jeremy Battle <jeremy@jeremybattle.com>
MIT Licensed
*/
/*
Module Dependencies
*/
var fs = require('fs')
, path = require('path')
;
/*
Variable to access to store routes before returning. Starts as an empty object
*/
var router = {};
/*
Function to escape characters in a string for use in a regex.
Credit: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions
*/
function escapeRegExp(string){
return string.replace(/([.*+?^${}()|\[\]\/\\])/g, "\\$1");
}
/**
* Take an array of strings and modify the router object to contain keys in the nested order
*
* @param {Array} arr
* @return {Object}
* @api private
*/
function objectPath(arr){
var ref = router;
arr.forEach(function(i){
if(typeof i === 'string' && i.length) {
//if the key doesn't exist, create it with an empty object
ref[i] = ref[i] || {};
//update our ref to match our current depth
ref = ref[i];
}
});
if(keyName.length){
if(router.hasOwnProperty(keyName)){
console.log('You appear to have both a JS and Coffee file named: "' + keyName + '." Routes may conflict.')
return ref;
}
/**
* Walks directories to find JS files containing routes to add to the router object
*
* @param {location} string
* @param {prefix} string
* @return {Object}
* @api private
*/
function recursiveWalk(location, prefix){
//the prefix is our original absolute location, we don't want that in our path
//so regex it out
var regex = new RegExp('^' + escapeRegExp(prefix));
//split our remaining path by /
var routerKeys = location.replace(regex, '').split('/');
//loop over the folder contents
fs.readdirSync(location).forEach(function(i){
var fullPath = path.resolve(location, i);
var item = fs.lstatSync(fullPath);
if(item.isDirectory()){
//keep going, we only care about files
recursiveWalk(fullPath, prefix);
} else if(item.isFile()){
//only try to require js files
if(fullPath.slice(-3) == '.js') {
var keyPath = objectPath(routerKeys);
try {
if (keyPath) {
keyPath[i.substr(0, i.length - 3)] = require(fullPath);
} else {
router[i.substr(0, i.length - 3)] = require(fullPath);
}
} catch(e){
console.error('Unable to load route: "%s" it will be skipped.', fullPath);
}
}
router[keyName] = require(routeFolder + '/' + file);
}
});
return router;
}
/**
* Takes a location and walks the directory tree to find routes
*
* @param {String} location
* @returns {Object}
* @api public
*/
module.exports = function(location){
if(location){
location = path.resolve(location);
} else {
//if no path was given, assume /routes location related to executing file
location = path.resolve(process.env.PWD, '/routes');
}
return recursiveWalk(location, location);
}

7

package.json
{
"name": "express-quick-routes",
"version": "0.0.0",
"version": "1.0.0",
"description": "A quick and easy way to include all your Express routes.",
"main": "index.coffee",
"main": "index.js",
"scripts": {

@@ -13,5 +13,2 @@ "test": "mocha"

},
"dependencies": {
"sugar": "*"
},
"devDependencies": {

@@ -18,0 +15,0 @@ "mocha": "*",

@@ -5,12 +5,22 @@ express-quick-routes

Version 1.0.0 introduces breaking changes to version 0.0.0. Please read the updated documentation for new usage.
Support for nested folders also added in 1.0.0
Usage
--------------------------------
By default, the module will look for the routes in the root of your project inside the /routes folder.
By default, the module will look for the routes in the root of your project inside the /routes folder relative to the
file being executed.
Assuming you have the folder structure:
\
\routes
\routes\index.js
\routes\auth.js
\
\routes
\routes\index.js
\routes\auth.js
The file \routes\index.js has the functions login() and logout() and the file

@@ -20,3 +30,4 @@ \routes\auth.js has the functions googleAuth() and googleCallback() then you could

```
```javascript
var express = require('express');

@@ -28,3 +39,4 @@ var app = express();

/* Setup your routes */
var router = require('express-quick-routes').init();
var quickRoutes = require('express-quick-routes');
var router = quickRoutes();
app.get('/auth/google', router.auth.googleAuth);

@@ -40,17 +52,17 @@ app.get('/auth/googleCallback', router.auth.googleCallback);

It's some what difficult for me to explain exactly how to use it, hopefully that was helpful. But the bottom line is
when you add files to the routes folder in your server root those javascript/coffeescript files are autoloaded into
the router object. Then you can easily access the functions.
If you don't want to store the routes in the default location you can pass an alternate location to the init() like so:
```javascript
var quickRoutes = require('express-quick-routes');
var router = quickRoutes('/opt/site/myRouteFolder');
```
var router = require('express-quick-routes').init('/opt/site/myRouteFolder');
```
or a relative path:
```javascript
var quickRoutes = require('express-quick-routes');
var router = quickRoutes('../../anotherRouteFolderLocation');
```
var router = require('express-quick-routes').init('../../anotherRouteFolderLocation');
```

@@ -57,0 +69,0 @@ If you have any questions or find any bugs please let me know and I'll be sure to address them.

var should = require('should')
, mocha = require('mocha')
, expect = require('chai').expect;
var quickRoutes = require('../index.js');
describe('testing route loading', function(){
describe('test router generation', function(){
it('should load the router with the available functions in the files contained in the routes folder', function(done){
var router = require('../index.js').init("./test/routes");
var router2 = require('../index.js').init("./test/routes2")
it('should load a router object structure matching that of routes/app', function(done){
var router = quickRoutes('./test/routes/app');
expect(router).to.contain.keys('auth', 'home');
expect(router.auth).to.contain.keys('index', 'register');
expect(router.auth.index).to.contain.keys('signin', 'signout');
expect(router.auth.index.signin).to.be.a('function');
expect(router.auth.index.signout).to.be.a('function');
expect(router.auth.register).to.contain.keys('index');
expect(router.auth.register.index).to.be.a('function');
expect(router.home).to.contain.keys('index');
expect(router.home.index).to.contain.keys('index', 'contact');
expect(router.home.index.index).to.be.a('function');
expect(router.home.index.contact).to.be.a('function');
if(!router.admin || !(typeof router.admin.index === 'function') || !(typeof router.admin.test === 'function')){
throw "Admin routes not found. Failed test.";
}
done();
})
if(!router.index || !(typeof router.index.index === 'function')){
throw "Main routes not found. Failed test.";
}
if(!router2.blog || !(typeof router2.blog.index === 'function') || !(typeof router2.blog.post === 'function')){
throw "Blog routes not found. Failed test.";
}
it('should load a router object structure matching that of routes/www', function(done){
var router = quickRoutes('./test/routes/www');
expect(router).to.contain.keys('dashboard', 'index');
expect(router.dashboard).to.contain.keys('index');
expect(router.dashboard.index).to.contain.keys('index');
expect(router.dashboard.index.index).to.be.a('function');
expect(router.index).to.contain.keys('about', 'home');
expect(router.index.about).to.contain.keys('index');
expect(router.index.about.index).to.be.a('function');
expect(router.index.home).to.contain.keys('index');
expect(router.index.home.index).to.be.a('function');
done();

@@ -24,0 +38,0 @@ });

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc