npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

injex-express-plugin

v1.0.0

Published

Injex plugin for Node JS Express applications

Downloads

2

Readme

injex-express-plugin

Turn your Express application into a dependency-injection ballet using Injex


npm version Build Status codecov

Why should you use this Injex plugin?

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

Install Injex Express Plugin using NPM or Yarn:

npm install --save injex-express-plugin

Or

yarn add injex-express-plugin

Basic usage

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.

Configurations

You can use the following plugin configurations:

app: Application

  • Pass in an express application instance if you already configured one.
    Default: 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}...`));
    			}
    		})
    	]
    });

Decorators

@controller()

  • Define a module as a controller.
    User this decorator on your controller class for example:

@get([path]), @post([path]), @post([path]), @patch([path]), @put([path]), @del([path])

  • Binds a controller method into Express application route.

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.


npm version Build Status codecov

Having an issue? A feature idea? Want to contribute?

Feel free to open an issue or create a pull request