New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

express-otto

Package Overview
Dependencies
Maintainers
1
Versions
2
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-otto

A lightning fast :zap: automatic router-registration solution for express applications.

unpublished
latest
Source
npmnpm
Version
0.1.1
Version published
Maintainers
1
Created
Source

Express Otter

A lightning fast :zap: automatic router-registration solution for express applications.

(Built with extensibility in mind)

:wave: Introduction

Automatically crawls your project in order to register routers to your express application. You can use this on older express projects or newer ones. Create static routes if you prefer managing your routes with code or create dynamic routes if you prefer a more modern approach.

Dynamic routing has become more and more popular lately --- and with so many benefits, why wouldn't it be?

It has:

  • A single source of truth for defining your routes
  • A visual representation of your routes
  • An easy way to change your routes when refactoring

:inbox_tray: Installation

npm install express-otter

:telescope: Usage

Given a project structure like so....

Dynamic routing uses the project structure to define router URLs.

root
├──dist
├──src
│  ├──middleware
│  ├──models
│  ├──routes <-------------- base path
│  │  └──pets
│  │     └──[pet].js <------ router
│  └──utils
└──tests
/** ./app.js */

import express from 'express'
import { registerRouters } from 'express-otter'

const app = express()

/** Register routers after initializing the app */
await registerRouters({
    app,
    path: ['./src/routes'] // Points to the routes directory
})

app.listen(3000, () => {
    console.log('Server running at http://localhost:3000')
})
/** ./src/routes/pets/[pets].js */

import express from 'express'
import { generateURL } from 'express-otter'

/** Used to generate the URL when registering routes */
const url = generateURL()
// '/pets/:pets'

const router = express.Router()

/** Router will automatically be registered */
router.get(url, (req, res) => {
    res.send('You have reached this endpoint!')
})

export default router
const response = await fetch('http://localhost:3000/pets/dog')
    .then(response => response.text())

console.log(response)
// 'You have reached this endpoint!'

Static Routing

Static routing uses the code to defined router URLs.

root
├──dist
├──src
│  ├──middleware
│  ├──models
│  ├──routes <-------------- base path
│  │  └──cars.js <---------- router
│  └──utils
└──tests
/** ./app.js */

import express from 'express'
import { registerRouters } from 'express-otter'

const app = express()

/** Register routers after initializing the app */
await registerRouters({
    app,
    path: ['./src/routes'] // Points to the routes directory
})

app.listen(3000, () => {
    console.log('Server running at http://localhost:3000')
})
/** ./src/routes/cars.js */

import express from 'express'

const router = express.Router()

/** Router will automatically be registered */
router.get('/cars/toyota/:model', (req, res) => {
    res.send('You have reached this endpoint!')
})

export default router
const response = await fetch('http://localhost:3000/pets/dog')
    .then(response => response.text())

console.log(response)
// 'You have reached this endpoint!'

:book: Documentation

registerRouters(options)

options
PropertyTypeDefaultDescription
appExpressAn express app.
pathstring[]Defines the base path(s) when looking for routers.
slug_patternRegExp/\[(.+)\]/gA global pattern for capturing slugs in files/folders.
ignore_patternRegExp/^_/A pattern for ignoring files/folders.
drybooleanfalseSpecifies whether to perform a dry run.
beforeRegisterfunctionA callback that is invoked right before registering a router.
afterRegisterfunctionA callback that is invoked right after registering a router.

:test_tube: Developement

Roadmap

  • Create static & dynamic routing
  • Allow for flexible slug & ignore pattern design
  • Add beforeRegister & afterRegister hooks
  • Add WebSocket support
  • Create plugin capabilities
  • Add more regex options for matching

Contributing

To install dependencies:

bun install

To run:

bun run index.ts

This project was created using bun init in bun v0.6.12. Bun is a fast all-in-one JavaScript runtime.

Keywords

express

FAQs

Package last updated on 09 Aug 2023

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