Coder Social home page Coder Social logo

express-api-routes's Introduction

npm version semantic-release

Build Status JavaScript Style Guide Code Climate

forthebadge forthebadge

forthebadge

Express.js Server Auto Routing

This module is designed to be used in a Node.js Express.js server. It allows you to build routes/endpoints by simply declaring functions (in your controller files). All functions in the controllers/myPath.js will be available at the endpoint /myPath/{functionName} - they are automatically bound to the express app router. It also allows you to define policy "middleware" that is processed before the request gets to your endpoint.

Node v4

npm install --save express-api-routes

TL;DR

- controllers/
  - user.js
- policies/
  - loggedIn.js
- config/
  - routes.js
- package.json
- app.js

/app.js

const app = require('express')();
const expressApiRoutes = require('expressApiRoutes');

// initialize with a config file
const api = new expressApiRoutes({

  // Logger (winston instance) {optional}
  logger: new winston.Logger,

  // Root directory
  root: __dirname, // defaults to process.mainModule.filename.dir

  // Base route {optional}
  baseRoute: '/api', // defaults to '/'

  // Controllers directory {required} absolute path to controllers dir
  controllers: __dirname + "/controllers", // default if none provided

  // Policies directory {optional} absolute path to policies dir
  policies: __dirname + "/policies", // default if none provided

  // Routes Config Object {optional}
  routes: require('./config/routes'),

  // Express instance {optional}
  app: app, // creates an express app if none provided

});

console.log(api); // { app: app, controllers: {..}, policies: {..}, routesMap: [..] }

app.listen(3000);

/controllers/user.js

module.exports = {
  index(req,res,next) {
    // localhost:3000/api/user
    return res.send('Woohoo!');
  },
  someOtherAction(req,res,next) {
    // localhost:3000/api/user/someOtherAction
    return res.send('action, bam!');
  }.
  getUsers(req,res,next) {
    // do some database work...
    return res.send('Send users list!');
  }
};

/policies/loggedIn.js

module.exports = (req,res,next) => {
  console.log('Policy checking login credentials');
  next();
};

/config/routes.js

module.exports = {
  '/users/list': {
    controller: 'user',
    method: 'getUsers',
    policies: ['loggedIn']
  }
};

Now going to your browser localhost:3000/api/users/list will run the function getUsers() from the controller file /controllers/user.js

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.