@ryanforever/express-utils
v1.0.6
Published
utils for express servers
Readme
express-utils
simple, basic utilities for express servers
- send
- sendStatus
- echoRequest
- basicAuth
- whitelist
- routeLogger
- forceHttps
- sendError
send
const {send} = require("@ryanforever/express-utils")
app.get("/hello", send("world", 200))sendStatus
const {sendStatus} = require("@ryanforever/express-utils")
app.get("/ping", sendStatus(200))routeLogger
const {routeLogger} = require("@ryanforever/express-utils")
const express = require("express")()
app.use(routeLogger({
ignore: ["/health"] // choose paths to ignore
}))echoRequest
const {echoRequest} = require("@ryanforever/express-utils")
app.get("/users", echoRequest)
// will send back {baseUrl, url, path, method, body, parms, query, headers}
/*
{
"baseUrl": "",
"url": "/echo?hello=world",
"path": "/echo",
"method": "GET",
"body": {
"name": "ryan forever"
},
"query": {
"hello": "world"
},
"params": {},
"headers": {
"host": "localhost",
"content-type": "application/json",
"user-agent": "insomnia/8.4.1",
"x-test-header": "abcd1234",
"content-length": "27"
}
}
*/basicAuth
forked from express-basic-auth
const {basicAuth} = require("@ryanforever/express-utils")
const app = require("express")()
app.use(basicAuth({
users: {"ryanforever": "abcd1234"},
challenge: true // use for popup password input
}))sendError
const {sendError} = require("@ryanforever/express-utils")
const app = require("express")()
// add method to express's response object
app.response.sendError = sendError
app.get("/error", (req, res) => {
res.sendError({
error: "something went wrong",
status: 400
})
})
// or use as a standalone function
app.get("/error", (req, res) => {
sendError({
error: "something went wrong",
status: 400,
res
})
})errorHandler
const {errorHandler} = require("@ryanforever/express-utils")
const app = require("express")()
// put this at the bottom of your middleware/route chain.
app.use(errorHandler({sendErrorMeessage: false}))errorResponse
an formatted error response object to send
const {errorResponse} = require("@ryanforever/express-utils")
// or use as a standalone function
app.get("/test", (req, res) => {
res.send(errorResponse({
error: "something went wrong",
status: 400,
})
})
/*
will send this JSON as response
{
"error": "something went wrong",
"status": 400,
"timestamp": "2025-06-28T11:05:55.287Z",
"metadata": {}
}
*/