Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

express-routes-as-object

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

express-routes-as-object

## Table of Contents

  • 1.0.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
7
Maintainers
1
Weekly downloads
 
Created
Source

express-routes-as-object

Table of Contents

  • Installing
  • Example
  • Credits
  • License

Installing

Using npm:

$ npm i express-routes-as-object

Using yarn:

$ yarn add express-routes-as-object

Example

Server

note: For now This package only covers for reast-api use case

//server.js

const express = require("express");

const customRoute = require("./config/routes");
const exrouteObject = require("express-routes-as-object");
const app = express();

app.use("/", (req, res) => {
  exrouteObject(req, res, customRoute);
});

const server = app.listen(4000, () =>
  console.log(`
🚀⭐️ Server ready at: http://localhost:4000`)
);

Custom routes

note

Every route configuration consists of an address and a target, for example:

'GET /foo/bar': {action: "/controllers/foo/bar"}
^^^address^^^   ^^^^^^^^^^^^target^^^^^^^^^^^^^^
// routes.js

module.exports = {
  routes: {
    "POST /account/create": {
      middleware: [firstMiddleware, secondeMiddleware],
      action: "/controllers/user/create",
    },
    "GET /account/find": {
      middleware: [checkResponse, greatePost],
      action: "/controllers/user/find-one",
    },
  },
};

function firstMiddleware(req, res, next) {
  console.log("this is the first middleware function");
  next();
}

function secondeMiddleware(req, res, next) {
  console.log("after the first middleware come the second middleware");
  next();
}

Controller file

Note:

Before using this package, you have to create a controller directory on the root of your project directory

📦Project
 ┣ 📂controllers
 ┃ ┗ 📂user
 ┃ ┃ ┗ 📜find-one.js
 ┣ 📂node_modules
 ┃ ┗ 📜...
 ┣ 📜server.js
 ┣ 📜package.json
 ┗ 📜routes.js

Every route configuration consists of an address and a target, for example:

module.exports = (req, res) => {
  res.send("Hello from express");
};
// /controllers/user/find-one.js
NOTE

The qs library is preferable if you need to stringify nested objects, as the querystring method has known issues with that use case (https://github.com/nodejs/node-v0.x-archive/issues/1665).

Credits

express-routes-as-object is heavily inspired by the Routes provided in sailsjs.com. Ultimately express-routes-as-object is an effort to provide same routing method in sailsjs to expressjs

License

MIT

FAQs

Package last updated on 24 Apr 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc