1.0.2 • Published 2 years ago

node-server-router v1.0.2

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

node-server-router

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.

NPM version js-standard-style Maintenance Documentation LICENSE

Prerequisites

As most NodeJS projects require, please ensure you have NodeJS installed; install here

Install

Using npm:

npm install node-server-router --save

Using yarn

yarn add node-server-router

Usage

ECMAScript Modules

Basic usage:

import express from "express";
import * as nsr from "node-server-router";

const app = express();
nsr.RouteFactory.applyRoutesTo(app);

app.listen(6969);

CommonJS

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);

Route Structure

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: [...]
    }
];

Documentation

Config

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
}

Options

Prop nameOptionaltypeDefaultDescription
route_diryesstringroutesThe 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_versionyesstring/API verison identifier. e.g '/v1' resulting in http://localhost:6969/v1/...
log_configuredyesbooleanfalseLogs 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" });
// ...

Examples

You can find simple examples for the 4 supported types of Node.js Servers:

License

MIT

1.0.2

2 years ago

1.0.1

3 years ago

1.0.0

3 years ago