This packages provides and easy way to create dynamic routing on your NodeJS & ExpressJS server. It allows you to add API route files to any directory and bind them all to the ExpressJS Application instance.
As most NodeJS projects require, please ensure you have NodeJS installed; install here
Using npm
:
npm install node-server-router --save
Using yarn
yarn add node-server-router
Basic usage:
import express from "express";
import * as nsr from "node-server-router";
const app = express();
nsr.RouteFactory.applyRoutesTo(app);
app.listen(6969);
Basic usage:
const nsr = require("node-server-router");
const app = require("express")();
nsr.RouteFactory.applyRoutesTo(app);
app.listen(6969);
Using options config settings:
const nsr = require("node-server-router");
const app = require("express")();
nsr.RouteFactory.applyRoutesTo(app, {
api_version: "/v1",
log_configured: true,
route_dir: "routes",
});
app.listen(6969);
All route files need to follow the following structure:
import { HTTPAction } from "node-server-router";
// @type {Array<Route>}
export default [
{
url: "/dog:id",
action: HTTPAction.GET,
handlers: [...]
}
];
Configuration will allow you to specify unique items to your server. All config properties are optional.
Example config structure:
const config = {
route_dir: "routes",
api_version: "/v1",
log_configured: false
}
Prop name | Optional | type | Default | Description |
---|---|---|---|---|
route_dir |
yes | string |
routes |
The folder were your routes reside. e.g if the passed values is routes it will be interpreted as ./routes ; /folder1/folder2 is ./folder1/folder2 . |
api_version |
yes | string |
/ |
API verison identifier. e.g '/v1' resulting in http://localhost:6969/v1/...
|
log_configured |
yes | boolean |
false |
Logs to the console when routes are successfully configured. |
Example usage of any or all combinations of config props:
// ...
nsr.RouteFactory.applyRoutesTo(app, { route_dir: "/services/main/routes" });
// ...
You can find simple examples for the 4 supported types of Node.js Servers:
- JavaScript:
- TypeScript: