
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
injex-express-plugin
Advanced tools
Turn your Express application into a dependency-injection ballet using Injex
When working with Express applications, one of the hassles is to config all your application routes and route handlers. This plugin helps organize your application by using controllers and decorators, each controller has it's own route handlers for a specific application domain.
Install Injex Express Plugin using NPM or Yarn:
npm install --save injex-express-plugin
Or
yarn add injex-express-plugin
const container = Injex.create({
rootDirs: [
"./controllers",
"./managers",
"./services",
],
...more injex config...
plugins: [
...more plugins...
new InjexExpressPlugin({
// plugin configurations...
})
]
})
Example Controller:
@define()
@controller()
export class ProductsController {
@inject() private productsManager: ProductsManager;
@get("/products")
public renderAllProductsPage(req, res) {
res.render("products", {});
}
@get("/products/:productId")
public async renderProductPage(req, res) {
const product = await this.productsManager.getProductById(req.params.productId);
res.render("product", {
product
});
}
@post("/products/create")
public async createProduct(req, res) {
const newProduct = await this.productsManager.create(req.body);
res.redirect(`/products/${newProduct.id}`);
}
}
Check out the example folder for a more detailed usage.
You can use the following plugin configurations:
app: Application
null
createAppCallback: CreateAppCallback
When no express application provided to the app
config, the express application will be created internally by the plugin, this callback will be called with the application instance so you can complete the app configurations.
Default: null
For example:
const PORT = process.env.PORT || 8080;
const container = Injex.create({
...injex config...
plugins: [
...more plugins...
new InjexExpressPlugin({
createAppCallback: function(app) {
// set app middlewares and/or any other configurations here...
app.listen(PORT, () => console.log(`App is running on ${PORT}...`));
}
})
]
});
@controller()
@get([path])
, @post([path])
, @post([path])
, @patch([path])
, @put([path])
, @del([path])
Controller example:
@define()
@controller()
export class HomeController {
@get("/")
public renderRoot(req, res) {
}
@get("/product/:id")
public renderProduct(req, res) {
}
@post("/product/create")
public createProduct(req, res) {
}
}
Is the same as the traditional express way:
app.get("/", function(req, res) {
});
app.get("/product/:id", function(req, res) {
});
app.post("/product/create", function(req, res) {
});
The difference is that with Injex, you can inject dependencies into your controller.
Another difference is the use of the @singleton()
decorator, as you can see from the example above, the HomeController is defined without it, it means that you will get a "fresh" HomeController instance for each request, you can call it a session controller. When using the @singleton()
decorator on a controller class, you get the same controller instance for each client request.
For example:
@define()
@singleton()
@controller()
export class HomeController {
private visitors: number;
constructor() {
this.visitors = 0;
}
@get("/")
public render(req, res) {
res.send(`<h1>This page visited ${++this.visitors} times!</h1>`);
}
}
And without the @singleton()
decorator:
@define()
@controller()
export class HomeController {
private visitors: number;
constructor() {
this.visitors = 0;
}
@get("/")
public render(req, res) {
res.send(`<h1>${++this.visitors} === 1</h1>`);
}
}
Check out the example folder for a more detailed usage.
Feel free to open an issue or create a pull request
FAQs
Injex plugin for Node JS Express applications
The npm package injex-express-plugin receives a total of 3 weekly downloads. As such, injex-express-plugin popularity was classified as not popular.
We found that injex-express-plugin 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
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.