![require(esm) Backported to Node.js 20, Paving the Way for ESM-Only Packages](https://cdn.sanity.io/images/cgdhsj6q/production/be8ab80c8efa5907bc341c6fefe9aa20d239d890-1600x1097.png?w=400&fit=max&auto=format)
Security News
require(esm) Backported to Node.js 20, Paving the Way for ESM-Only Packages
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
express-simple-controllers
Advanced tools
Create super simple controllers for routing an express app.
Create super simple controllers for routing an express app.
First off, initialize the controllers
import initialize from "express-simple-controllers";
import express from "express";
const router = express.Router();
initialize(router);
Then create a controllers
directory and add anything with an _controller.js
suffix. e.g. home_controller.js
.
Anything with that suffix will be added as a controller.
If you need to change the controller directory...
initialize(router, { directory: "server/controllers" });
You can create controller methods in a few different ways.
const show = {
method: "GET",
route: "/home",
handler(req, res, next) {
res.render("home");
}
};
export { show };
const show = {
method: "GET",
route: "/home",
handler: {
json(req, res, next) {
res.json({ home: "home" });
}
html(req, res, next) {
res.render("home");
}
}
};
export { show };
If you export a before
function, it will run before every method in the file unless you pass a skipBefore: true
.
const before = (req, res, next) => {
req.something = "a thing";
next();
}
const show = {
method: "GET",
route: "/home",
handler(req, res, next) {
const { something } = req;
res.render("home", { something });
}
};
const showNoBefore = {
method: "GET",
route: "/home",
skipBefore: true,
handler(req, res, next) {
// ...
}
};
export {
before,
show,
showNoBefore,
};
You can also pass an array of of middleware.
const show = {
method: "GET",
route: "/home",
middleware: [first, second(someOptions)],
handler(req, res, next) {
res.render("home");
}
};
export { show };
You can also create a simple crud controller by creating named functions in the exports...
// user_controller.js
const before = (req, res, next) => next();
// GET /user
const show = (req, res, next) => {};
// GET /users
const list = (req, res, next) => {};
// PUT /user
const update = (req, res, next) => {};
// POST /user
const create = (req, res, next) => {};
// DELETE /user
const delete = (req, res, next) => {};
export {
show,
list,
update,
create,
delete
};
The prefix of the file sets the route, in the above case /user
because the controller is named user_controller.js
.
Will build to the /dist
directory.
npm run build
npm run test
npm run test:ci # Runs in watch mode
FAQs
Create super simple controllers for routing an express app.
The npm package express-simple-controllers receives a total of 5 weekly downloads. As such, express-simple-controllers popularity was classified as not popular.
We found that express-simple-controllers demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Security News
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
Security News
PyPI now supports iOS and Android wheels, making it easier for Python developers to distribute mobile packages.
Security News
Create React App is officially deprecated due to React 19 issues and lack of maintenance—developers should switch to Vite or other modern alternatives.