You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 4-6.RSVP
Socket
Book a DemoInstallSign in
Socket

express-router-handler

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install
Package was removed
Sorry, it seems this package was removed from the registry

express-router-handler

Handle routes in a simpler way.

1.0.4
unpublished
Source
npmnpm
Version published
Weekly downloads
0
Maintainers
1
Weekly downloads
 
Created
Source

Express Router Handler

An Express js routes handler that will automatically generate API routes for you based on the folder structure.

Folder structure example (as nested as you want it!):

RouterHandler options:

app - your express server instance.

routesPath - the path to your routes folder - Use the path library to define this.

basePath - this changes your API routing. For example: By default your requests could look something like this:

  • http://localhost:3000/users

Adding a basePath like /api will change the routing to look like this:

  • http://localhost:3000/api/users

IMPORTANT: Adding a trailing slash to basePath will affect your routing.

CommonJS Example:

const path = require('path');
const express = require('express');
const RouterHandler = require('express-router-handler');

const app = express();
const PORT = 3000;

new RouterHandler({
  app,
  // IMPORTANT: Use the path library to define routesPath
  routesPath: path.join(__dirname, 'routes'),
  // basePath: '/api', // optional
});

app.listen(PORT, () => {
  console.log(`Server running on port ${PORT}`);
});

Typescript Example:

import express, { Application } from 'express';
import RouterHandler from 'express-router-handler';
import path from 'path';

const app: Application = express();
const PORT = 3000;

new RouterHandler({
  app,
  // IMPORTANT: Use the path library to define routesPath
  routesPath: path.join(__dirname, 'routes'),
  // basePath: '/api', // optional
});

app.listen(PORT, () => {
  console.log(`Server running on port ${PORT}`);
});

Your route file needs to export a function

CommonJS example:

/routes/users/:id/get.js

module.exports = (req, res) => {
  const id = req.params.id;
  res.send(`User id: ${id}`);
};

TypeScript example:

/routes/users/:id/get.js

import { Request, Response } from 'express';

module.exports = (req: Request, res: Response) => {
  const id = req.params.id;
  res.send(`User id: ${id}`);
};

ES6 JS currently has a few bugs related to imports/exports so I wouldn't recommend using it. So far I've tested in both CommonJS and TypeScript with the above examples and it has no issues.

Keywords

express

FAQs

Package last updated on 26 Dec 2022

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.