mx-file-router
Version:
A simple file-based router for NodeJS
100 lines (74 loc) • 2.95 kB
Markdown

Fast, light-weight file based routing solution for [Node.js](http://nodejs.org).
[![NPM Version][npm-version-image]][npm-url]
[![NPM Install Size][npm-install-size-image]][npm-install-size-url]
[![NPM Downloads][npm-downloads-image]][npm-downloads-url]
---
> **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)`
---
```js
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
})
```
| 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:
```js
// (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>')
}
```
Before installing, you will have to [download and install Node.js](https://nodejs.org/en/download/).
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](https://docs.npmjs.com/creating-a-package-json-file).
**Installing the package**
```console
$ npm install mx-file-router
$ npm update
```
**Dependencies**
[](https://www.npmjs.com/package/express) will be automatically installed as a dependency.
More information on installing npm packages can be found [here](https://docs.npmjs.com/getting-started/installing-npm-packages-locally).
* Low-code setup
* Easy to use
* Low file size
[](LICENSE)
[]: https://badgen.net/npm/dm/mx-file-router
[]: https://npmcharts.com/compare/mx-file-router?minimal=true
[]: https://badgen.net/packagephobia/install/mx-file-router
[]: https://packagephobia.com/result?p=mx-file-router
[]: https://npmjs.org/package/mx-file-router
[]: https://badgen.net/npm/v/mx-file-router