This package has been deprecated

Author message:

Package no longer supported. Contact Support at https://www.npmjs.com/support for more info.

express-routes-as-object

1.0.2 • Public • Published

express-routes-as-object

Table of Contents

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

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

Package Sidebar

Install

npm i express-routes-as-object

Weekly Downloads

0

Version

1.0.2

License

MIT

Unpacked Size

5.24 kB

Total Files

4

Last publish

Collaborators

  • copy-ninja