express-extensions

1.0.1 • Public • Published

Express Extensions

Small extensions to Express

Async Router

Define async routes:

const express = require('express')
const { asyncRouter, respond } = require('express-extensions')
const getAccountInfo = require('./getAccountInfo')

const app = express()
const router = asyncRouter()

router.$get('/accounts/:accountId', async (req, res) => {
  const { accountId } = req.params
  const accountInfo = await getAccountInfo(accountId)
  respond(accountInfo, res) // responds with 404 if accountInfo is falsey.
})

app.use(router)

Start/Stop server

If your HTTP server has open connections (such as WebSocket or EventSource), server.close will hang. This isn't good for tests. The WebServer class will destroy those connections first, preventing this.

WebServer also provides a Promise API for starting and stopping the server, and also lets you choose a random port:

const express = require('express')
const { WebServer } = require('express-extensions')
const app = express()

const webServer = new WebServer(app)
const port = await webServer.listen(9988) // use 0 to select a random port

await webServer.stop() // This will kill keep-allive connections too

Readme

Keywords

none

Package Sidebar

Install

npm i express-extensions

Weekly Downloads

2

Version

1.0.1

License

MIT

Unpacked Size

4.79 kB

Total Files

9

Last publish

Collaborators

  • aslakhellesoy