Giter Club home page Giter Club logo

express-auto-routes's Introduction

Express Auto Routes

NPM version Downloads build status Dependency Status Coverage Status

中文介绍请点击 这里

In most of the time, we need to mount handlers / controllers to routes manually.

e.g.

/** now we are in routes/index.js **/
var homeRouter = require('./home/'),
  userCtrl = require('../controllers/user');

// Method 1: mount a sub router to a path. kinda code splitting but still complicated
app.use('/', homeRouter);

// Method 2: mount controllers to routes one by one, sucks
app.get('/user', userCtrl.index);
app.post('/user/login', userCtrl.login);
app.get('/user/logout', userCtrl.logout);
...

It's hardly elegant, so here comes express-auto-routes.
Firstly, npm install express-auto-routes --save

e.g.

/** now we are in app.js **/
var path = require('path'),
  express = require('express'),
  app = express();

var autoRoutes = require('express-auto-routes')(app); // you don't need `routes` folder any more
autoRoutes(path.join(__dirname, './controllers')); // auto mounting... done!

// ...other configures

app.listen(8080);
/** now we are in controllers/hello/world.js **/
exports.get = function (req, res, next) {
  res.send('hello world');
};

Then visit localhost:8080/hello/world, you will see hello world


The magic is just globbing all the valid controller files and resolve them based on relative path.
Since we glob files recursively, without doubt it supports unlimited sub folders.

e.g.

controllers/a/b/c/d/e/f/g.js => localhost:8080/a/b/c/d/e/f/g

Here I highly recommend you checking out the example/ folder for more detail.

> npm install
> npm run example

[AutoMount] use /home/
[AutoMount] use /user/
[AutoMount] use /user/logout/
[AutoMount] all /user/setting/
[AutoMount] get /home/foo/bar/world/
[AutoMount] get /user/setting/:uid
[AutoMount] delete /user/logout/:uid
[AutoMount] get /home/foo/hello/
[AutoMount] get /home/foo/bar/
[AutoMount] get /user/logout/
[AutoMount] get /user/:uid?
[AutoMount] get /home/foo/
[AutoMount] get /home/xyz/
[AutoMount] post /setting
[AutoMount] get /login
[AutoMount] get /home/
[AutoMount] post /login
[AutoMount] put /user/
[AutoMount] post /log
[AutoMount] get /

then visit localhost:8080 and test the above APIs with Postman


[ A more practical project ] checkout A Simple Message Board RESTful API which based on this package.

express-auto-routes's People

Watchers

shangwenhe avatar

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.