0.2.0 • Published 4 years ago

express-auto-mount v0.2.0

Weekly downloads
-
License
ISC
Repository
-
Last release
4 years ago

Express Auto Mount

An express package to mount routes and middleware automatically from file system.

Installation

npm install express-auto-mount --save

Getting started

const express = require('express')
const eam = require('express-auto-mount');
const path = require('path');

const app = express()

app.use(eam({ path: path.resolve(__dirname, 'routes') }));

app.listen(3000);

In that routes folder, create files ending with <http_method>.js and exporting the function that will handle the request. You can use any of the HTTP methods supported by Express. They need to be in lowercase in the file name, though.

For example, the following code in routes/api/users.get.js will make GET requests to /api/users respond with 'Users'.

module.exports = (req,res) => {
  res.send('Users');
}

The middleware also support file middleware.js. For example routes/api/middleware.js will respond to requests sent to /api/users.

module.exports = (req,res, next) => {
  console.log('middleware');
  next();
}

or Array a functions

const middleware1 = (req, res, next) => {
  console.log('middleware1');
  next();
}

const middleware2 = (req, res, next) => {
  console.log('middleware2');
  next();
}

module.exports = [middleware1, middleware2];
How to add routes and middlewares

To add roads, you need to create a roads folder at the root of your project.

routes /
  - index.get.js
  - middleware.js (register middleware(s) current path and all path level down)
  - api /
    - middleware.js (remove registered middleware(s) and register new middleware(s) current path and all level down)
    - users /
      - index.get.js
      - _id /
        - index.get.js
        - edit.post.js
        - middleware.add.js (not remove last middleware(s) and register more middleware(s) current path and all level down)

The routes obtained are as follows:

/
/api/users
/api/users/:id
/api/users/:id/edit
0.2.0

4 years ago

0.1.0-b2

4 years ago

0.1.0-b3

4 years ago

0.1.0

4 years ago

0.1.0-b1

4 years ago

0.0.1-build4

4 years ago

0.0.1-build2

4 years ago

0.0.1-build3

4 years ago

0.0.1-build1

4 years ago

0.0.1-b1

4 years ago

0.0.1-a

4 years ago

0.0.1

4 years ago