1.0.10 • Published 3 years ago

express-directory-router v1.0.10

Weekly downloads
-
License
ISC
Repository
github
Last release
3 years ago

express-directory-router

npm version

express-directory-router lets you build your express router using a directory tree with next to no boilerplate and without losing any features from express.

Inspired by Next router.

You can find full examples here

Installation

npm install --save express-directory-router

Basic Usage

DIRECTORY STRUCTURE
-------------------
* app.js
- routes
  * todos.js
  - todo
    * index.js (index files are used to create root level routes of path, same as creating todo.js)
    * :todoId.js (prefix filename with ":" for dynamic routes)

ROUTES GENERATED
----------------
GET /todos
GET /todo
POST /todo
GET /todo/:todoId
PUT /todo/:todoId
DELETE /todo/:todoId
/* app.js */
const express = require('express');
const { createDirRouter } = require('../src/');

const runApp = async () => {
  const app = express();

  // Promised based as it generates routes at runtime
  const routes = await createDirRouter('routes');

  app.use(routes);
  app.listen(3000, () => console.log('Magic happening on port 3000'));
};

runApp();
/* routes/todos.js */
exports.get = (req, res, next) => {...}
/* routes/todo/index.js */
exports.get = (req, res, next) => {...}
exports.post = (req, res, next) => {...}
/* routes/todo/:todoId.js */

// Accepts a single or an array of middleware
exports.get = [(req, res, next) => {...}, (req, res, next) => {...}]
exports.put = (req, res, next) => {...}
exports.delete = (req, res, next) => {...}
1.0.2

3 years ago

1.0.10

3 years ago

1.0.9

3 years ago

1.0.8

3 years ago

1.0.7

3 years ago

1.0.6

3 years ago

1.0.5

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago