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

@paciolan/express-easy-routes

v1.0.13

Published

[Convention over Configuration](https://en.wikipedia.org/wiki/Convention_over_configuration) library to load Express Routes without having to configure each route.

Downloads

450

Readme

Route Icon Express Easy Routes

Convention over Configuration library to load Express Routes without having to configure each route.

This library is designed to have a perfect balance between configuration and flexibility.

The Problem

Each route requires manual configuration with the app. This can become verbose.

const routes = require("@paciolan/express-easy-routes");
const cors = require("cors");
const express = require("express");
const app = express();

const port = 8080;

app.use(express.json()); // ❌ NO!
app.use(express.static()); // ❌ NO!
app.use(cors()); // ❌ NO!

// ❌ NO!
app.get("/", (req, res) => {
  res.send("Hello World!");
});

// ❌ NO!
app.get("/route-1", (req, res) => {
  res.send("Hello Route1!");
});

// ❌ NO!
app.get("/route-2", (req, res) => {
  res.send("Hello Route2!");
});

app.listen(port, () => console.log(`Listening on http://localhost:${port}`));

The Solution

Put middlewares and routes in specified directories (by Convention), then load all routes from those directories.

Now adding a route no longer requires manually configuration.

const routes = require("@paciolan/express-easy-routes");
const express = require("express");
const app = express();

const port = 8080;

// ✅ YES!
routes({ app, path: __dirname + "/middlewares/**/*.middleware.js" });
routes({ app, path: __dirname + "/controllers/**/*.controller.js" });

app.listen(port, () => console.log(`Listening on http://localhost:${port}`));

Install

npm install @paciolan/express-easy-routes

Example Routes

middleware/cors.middleware.js

In this example, I am going to add the CORS middlware.

const express = require("express");
var cors = require("cors");
const router = express.Router();

router.use(cors());

module.exports = {
  order: 100, // optional
  router
};

That's it! We just added CORS into our express app!

controllers/index.controller.js

Next let's create our main controller route.

const express = require("express");
const router = express.Router();

router.get("/", (req, res) => {
  res.send("Hello World!");
});

module.exports = {
  order: 100, // optional
  router
};

middlewares/profiler.middleware.js

Create a profiler that will first, create a timer, then finally log out the time.

note: middleware that needs to run first, should have a low order.

const express = require("express");
const router = express.Router();

router.use((req, res, next) => {
  const start = process.hrtime();

  res.once("finish", () => {
    const [seconds, nanoseconds] = process.hrtime(start);
    const milliseconds = (seconds * 1000) + (nanoseconds / 1000000); // prettier-ignore

    console.info(
      `${req.method} ${req.url} ${res.statusCode} ${milliseconds}ms`
    );
  });

  next();
});

module.exports = {
  order: 1,
  router
};

It is now easy to find the route in your directories. Copy and Paste to move routes from one project to another.

src/
  controllers/
    index.controller.js
  middlewares/
    cors.middleware.js
    profiler.middleware.js
  app.js

Unit Testing

Express Easy Routes also makes testing routes easy.

Create the file controllers/__tests__/index.controller.test.js.

const { response: mockRes } = require("jest-mock-express");
const { router } = require("../index.route");

describe("route /", () => {
  test("returns 'Success!'", () => {
    const req = { method: "GET", url: "/" };
    const res = mockRes();
    router(req, res);
    expect(res.send).toHaveBeenCalledWith("Success!");
  });
});

API

RouteConfig - Config passed into the routes function.

| Property | Required | Description | | -------- | :------: | --------------------------------------------------------------------- | | app | ✅ | Express app object. (the one returned from calling express()). | | path | ✅ | Absolute (full) glob to routes. |

RouteModule - Exports for the route files.

| Property | Required | Description | | -------- | :------: | -------------------------------- | | router | ✅ | Express Router object. | | order | | Sort (Ascending) order of route. |

Contributors

Joel Thoms (https://twitter.com/joelnet)

Icons made by Freepik from www.flaticon.com is licensed by CC 3.0 BY