Using npm:
$ npm i express-routes-as-object
Using yarn:
$ yarn add express-routes-as-object
//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`)
);
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();
}
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
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