Skip to content
This repository has been archived by the owner on Apr 9, 2024. It is now read-only.

cucumber-ltd/express-extensions

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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