npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@studiowebux/route

v6.0.0-alpha

Published

Route generator, static resources and custom responses

Downloads

4

Readme

Introduction

This module has 3 features :

  1. Load express routes automatically using a JSON configuration.
  2. Load static routes automatically using a JSON configuration.
  3. Add custom express responses.

This module is built to be use with Express.

For more details (EN/FR) : Wiki

Installation

npm install --save @studiowebux/route

NPM

Usage

Configuration

| Key | Value | Description | More info | | --------- | --------------------------------------------------------- | ----------- | --------- | | routes | RestAPI definition, see below for the structure. | | | | resources | static resources definition, see below for the structure. | | |

Example:

const opts = {
  routes: {
    "/": {
      resources: {
        "/": [
          {
            method: "get",
            middlewares: [], // By default, this route is publicly available, you should create a middleware to protect this resource.
            action: (req, res, next) => {
              return res.success({
                msg: "Welcome ! The Documentation is available here : /api/",
              });
            },
          },
        ],
        "/healthcheck": [
          {
            method: "get",
            middlewares: [], // By default, this route is publicly available, you should create a middleware to protect this resource.
            action: (req, res, next) => {
              return res.success({ msg: "Pong !" });
            },
          },
        ],
      },
    },
    "/user": {
      resources: {
        "/": [
          {
            method: "get",
            middlewares: [isAuthenticated()],
            action: require(path.join(__dirname, "actions", "user", "find"))
              .route,
          },
          {
            method: "post",
            middlewares: [],
            action: require(path.join(__dirname, "actions", "user", "create"))
              .route,
          },
        ],
        "/:id": [
          {
            method: "get",
            middlewares: [isAuthenticated()],
            action: require(path.join(__dirname, "actions", "user", "findOne"))
              .route,
          },
          {
            method: "put",
            middlewares: [isAuthenticated()],
            action: require(path.join(__dirname, "actions", "user", "update"))
              .route,
          },
          {
            method: "delete",
            middlewares: [isAuthenticated()],
            action: require(path.join(__dirname, "actions", "user", "remove"))
              .route,
          },
        ],
      },
    },
  },
  resources: [
    {
      path: "/public",
      resource: path.join(__dirname, "public"),
    },
    {
      path: "/img",
      resource: path.join(__dirname, "images"),
    },
  ],
};

Functions

constructor(opts, log = console)

Initialize the configurations globally.

const WebuxRoute = require("@studiowebux/route");
const webuxRoute = new WebuxRoute(opts, console);

The opts parameter is optional, You can skip the routes and resources definition and pass the configurations manually using the LoadRoute & LoadStatic function.

The log parameter allows to use a custom logger, by default it uses the console.

LoadRoute(router, routes = null): Promise

It loads the RestAPI routes automatically using a JSON configuration.

The JSON structure
  • The option must be an object.
  • The first object is the parent route
    • Like http://webuxlab.com/api/v1/healthcheck, where the last / is the parent and healthcheck the specific route.
  • The parent route has one key named resources
    • This object can have multiple specific routes
      • Each specific route has 3 keys
        • method: GET, POST, PATCH, DELETE, PUT, OPTIONS
        • middlewares: An array of middlewares to use on the specific route, the function will be use in the same order.
        • action: Can be the actual function using the express route definition ((req, res, next)=>{...}) or a require that export the route (like the user route).
  • To use the request parameter, simply use the : notation

Using the configuration provided by the module,

const express = require("express");
const app = express();
const router = express.Router();

webuxRoute.LoadRoute(router);

app.use("/", router);

With the configuration in parameter,

const routes = {
  "/": {
    resources: {
      "/": [
        {
          method: "get",
          middlewares: [], // By default, this route is publicly available, you should create a middleware to protect this resource.
          action: (req, res, next) => {
            return res.success({
              msg: "Welcome ! The Documentation is available here : /api/",
            });
          },
        },
      ],
      "/healthcheck": [
        {
          method: "get",
          middlewares: [], // By default, this route is publicly available, you should create a middleware to protect this resource.
          action: (req, res, next) => {
            return res.success({ msg: "Pong !" });
          },
        },
      ],
    },
  },
  "/user": {
    resources: {
      "/:id": [
        {
          method: "put",
          middlewares: [isAuthenticated()],
          action: require(path.join(__dirname, "actions", "user", "update"))
            .route,
        },
      ],
    },
  },
};

webuxRoute.LoadRoute(router, routes);
app.use("/", router);

The routes parameter is only used when you want to load a different configuration than the one present in the module configuration.In other words, it allows to use multiple configuration besides the global one. The router parameter is provided by Express.

LoadStatic(app, express, resources = null): Promise

It loads Static resources routes automatically using a JSON configuration,

The JSON structure
  • The option must be an array
  • each element has 2 keys.
    • path: The external path to access the resources.
    • resource: The path on the server where the resources are stored.
Usage

Using the configuration provided by the module,

const express = require("express");
const app = express();
const webuxRoute = new WebuxRoute(opts, console);

webuxRoute.LoadStatic(app, express);

With the configuration in parameter,

const express = require("express");
const app = express();
const webuxRoute = new WebuxRoute();

const resources = [
  {
    path: "/public",
    resource: path.join(__dirname, "public"),
  },
  {
    path: "/img",
    resource: path.join(__dirname, "images"),
  },
];

webuxRoute.LoadStatic(app, express, resources);

The resources parameter is only used when you want to load a different configuration than the one present in the module configuration.In other words, it allows to use multiple configuration besides the global one. The app & express parameters are provided by express.

LoadResponse(app): Void

It loads the custom responses and attaches them to the res object from Express.

const express = require("express");
const app = express();

webuxRoute.LoadResponse(app);
Custom responses Usage with res
app.get("/success", (req, res) => {
  res.success({ message: "success" }, "success", "success");
});

app.get("/created", (req, res) => {
  res.created({ message: "created" }, "created", "created");
});

app.get("/updated", (req, res) => {
  res.updated({ message: "updated" }, "updated", "updated");
});

app.get("/deleted", (req, res) => {
  res.deleted({ message: "deleted" }, "deleted", "deleted");
});

app.get("/forbidden", (req, res) => {
  // msg, devMsg
  res.forbidden();
});

app.get("/badrequest", (req, res) => {
  // msg, devMsg
  res.badRequest();
});

app.get("/servererror", (req, res) => {
  // msg, devMsg
  res.serverError();
});

app.get("/notFound", (req, res) => {
  // msg, devMsg
  res.notFound();
});

app.get("/unprocessable", (req, res) => {
  // msg, devMsg
  res.unprocessable();
});

app.get("/custom", (req, res) => {
  res.custom(200, { message: "Custom  response", user: "User Name" });
});

Quick start

Complete example

Step 1. Directories creation

| Répertoire | Description | | ---------- | ------------------------------------------------------------------------------------ | | actions/* | The application logic (See the example/actions directory to see all possibilities) | | images | It stores the images | | public | It stores the public resources, like html files and others | | config.js | The routes and resources JSON configuration | | index.js | The server using ExpressJS |

Step 2. Action example

actions/user/find.js

const route = async (req, res, next) => {
  return res.success({ msg: "Find User", user: { fullname: "John Doe" } });
};

module.exports = { route };

Step 3. The configuration

config.js

const path = require("path");

// Include the middlewares somehow...
const isAuthenticated = () => {
  return (req, res, next) => {
    console.log("The user must be authenticated to execute the action...");
    return next();
  };
};

module.exports = {
  routes: {
    "/": {
      resources: {
        "/": [
          {
            method: "get",
            middlewares: [], // By default, this route is publicly available, you should create a middleware to protect this resource.
            action: (req, res, next) => {
              return res.success({
                msg: "Welcome ! The Documentation is available here : /api/",
              });
            },
          },
        ],
        "/healthcheck": [
          {
            method: "get",
            middlewares: [], // By default, this route is publicly available, you should create a middleware to protect this resource.
            action: (req, res, next) => {
              return res.success({ msg: "Pong !" });
            },
          },
        ],
      },
    },
    "/user": {
      resources: {
        "/": [
          {
            method: "get",
            middlewares: [isAuthenticated()],
            action: require(path.join(__dirname, "actions", "user", "find"))
              .route,
          },
        ],
      },
    },
  },
  resources: [
    {
      path: "/public",
      resource: path.join(__dirname, "public"),
    },
    {
      path: "/img",
      resource: path.join(__dirname, "images"),
    },
  ],
};

Step 4. Server File

index.js

const WebuxRoute = require("../src/index");
const express = require("express");
const app = express();
const router = express.Router();
const options = require("./config");

const webuxRoute = new WebuxRoute(options, console);

(async () => {
  await webuxRoute.LoadResponse(app);
  await webuxRoute.LoadRoute(router);

  app.use("/", router);

  // must be added after load the REST API routes.
  await webuxRoute.LoadStatic(app, express);

  app.listen(1337, () => {
    console.log("Server is listening on port 1337");
  });
})();

Videos and other resources

Contribution

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

license

SEE LICENSE IN license.txt