mx-file-router
v1.3.1
Published
A simple file-based router for NodeJS
Downloads
25
Maintainers
Readme
Fast, light-weight file based routing solution for Node.js.
Version notices
v1.2.0beta + Added support for CJS files
(lightly tested, might not work as expected)
v1.1.0beta + /routes subfolder support
(not tested, might not work as expected)
Usage
const express = require('express')
const Router = require('mx-file-router')
const app = express()
Router.start(app, {
port: 3000, // default: 3000
controllers: 'controllers', // default: 'controllers'
extraLogs: true // default: false
})
Example methods
| Method | File | | ------ | ------ | | GET | home.get.js | | HEAD | home.head.js | | POST | home.post.js | | PUT | home.put.js | | DELETE | home.delete.js | | CONNECT | home.connect.js | | OPTIONS | home.options.js | | TRACE | home.trace.js | | PATCH | home.patch.js |
To create a route, first create a folder in the project root named controllers
.
After that, you can create .js
files like the examples above.
Write the following code inside the newly created route file:
// (REQUIRED) Point one or more endpoint(s) to the same location.
exports.endpoints = ['/']
// (OPTIONAL) Easily add middleware to your route.
exports.middleware = authMiddleware.isAdmin()
// (REQUIRED) The handler of this file.
exports.handler = async (req, res) => {
res.status(200).send('<h1>Homepage</h1>')
}
Installation
Before installing, you will have to download and install Node.js. Node.js v10.0 or higher is required.
If this is a new project, you will have to create a package.json
.
This can easily be done with the npm init
command.
Installing the package
$ npm install mx-file-router
$ npm update
Dependencies Express will be automatically installed as a dependency.
More information on installing npm packages can be found here.
Features
- Low-code setup
- Easy to use
- Low file size