2.1.0 • Published 5 years ago

expressjs-router v2.1.0

Weekly downloads
8
License
MIT
Repository
github
Last release
5 years ago

Express.js Router Build Status

Structured routing for Express.JS

Example

Every route is implemented in a dedicated file

Example:

.
+-- routes
|   +-+ todos
|     +-- create.js
|     +-- update.js
|     +-- delete.js
+-- models
|   +-- todo.js
+-- server.js

Example route definition routes/todos/create.js

// File: ./routes/todos/create.js

'use strict';


const Todo = require("../../models/todo");



// The validate function is executed before the responder, allowing
// to easily check for the existence of parameters and their format.
// If some parameters are not valid, the function must throw an error
// 
// Note: Can be either synchronous or asynchronous.
function validate(req) {
  if (!req.body.text) {
    throw new Error('Missing parameter "text"')
  }
}


// The respond method is the actual implementation of our route
async function respond(req, res, next) {
  let { text } = req.body;
  let user = req.preloadedData.user;
  
  let todo = new Todo({
    text,
    user,
  });
  
  await todo.validate();
  await todo.save();
  
  res.status(201);
  res.json({ todo });
}



// The exports describe the route definition.
// .path, .method and .respond are required
// 
// Conditions are checked to define wether the route should be
// executed or not. You can define as many conditions as you want.
export default {
  path: '/todos',
  method: 'POST',

  validate
  respond,
  
  conditions : [ 'isLoggedIn' ],
  preload    : [ 'user' ],
};

Express app server.js

// File: ./app.js

'use strict';

import express from 'express'; 
import router from 'expressjs-router';

const app = express();


// By enabling debug mode, routes will be logged to stdout
router();

// Conditionals allow us to add handy condition checks all over our app
router.createConditional('isLoggedIn', async (req) => {
  return !!req.session.user;
});

// Preloaders allow us to easily preload resources before the requests are executed
router.createPreloader('user', async (req) => {
  return await User.findById(req.session.user._id);
});


// You can decide if either you want to preload resources sequentially or in parallel
//
// 0 -> parallel (default)
// 1 -> sequential
//
router.setPreloadingMode(1);


// Finally, create routes by padding the path where they're stored
router.create(app, "./path/to/routes");


app.listen(80);

Remarks:

  1. Routes prefixed by _ or named index.js won't be included
  2. You can enable debug mode by calling router.enableDebug();
  3. Route creation is recursive.

Changelog:

2.0.0

  • Removed express-validator
  • Made validate function support asynchronous execution (using Promises)
  • Added ALL to match any HTTP method
  • Allowed method to be an array of HTTP methods

License:

MIT

2.1.0

5 years ago

2.0.1

5 years ago

2.0.0

8 years ago

2.0.0-rc4

8 years ago

2.0.0-rc3

8 years ago

2.0.0-rc2

8 years ago

2.0.0-rc1

8 years ago

2.0.0-rc

8 years ago

1.2.9

9 years ago

1.2.8

9 years ago

1.2.7

9 years ago

1.2.6

10 years ago

1.2.5

10 years ago

1.2.4

10 years ago

1.2.3

10 years ago

1.2.2

10 years ago

1.2.1

10 years ago

1.2.0

10 years ago

1.1.5

10 years ago

1.1.4

10 years ago

1.1.3

10 years ago

1.1.2

10 years ago

1.1.1

10 years ago

1.1.0

10 years ago

1.0.9

10 years ago

1.0.8

10 years ago

1.0.7

10 years ago

1.0.6

10 years ago

1.0.5

10 years ago

1.0.4

10 years ago

1.0.3

10 years ago

1.0.2

10 years ago

1.0.1

10 years ago

1.0.0

10 years ago