node-server-router
TypeScript icon, indicating that this package has built-in type declarations

1.0.2 • Public • Published

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 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" });
// ...

Examples

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

License

MIT

Readme

Keywords

Package Sidebar

Install

npm i node-server-router

Weekly Downloads

0

Version

1.0.2

License

MIT

Unpacked Size

23.8 kB

Total Files

25

Last publish

Collaborators

  • carterjcobb