@gibme/webserver
v20.0.2
Published
A simple express.js webserver wrapper with extra magic built in
Readme
Simple Express.js Application Wrapper
This wrapper makes it very easy to construct a new instance of an Express.js web server application using HTTP or HTTPs.
Features include:
- Preloaded recommended headers (optional)
- Automatic 404 handling (optional)
- Compression (optional)
- Auto-parsing of request bodies (all optional)
- JSON
- Raw
- Text
- URLEncoded
- XML
- Automatically attempts to decode the authorization header into
request.authorization- Basic
- Bearer
- JWT
- Provides a logging facility for requests or can specify a callback to receive request entries
- Automatically resolves a number of different proxy-related fields for the client IP address into
request.remoteIpin the following order:Cf-Connecting-IPv6X-Forwarded-ForCf-Connecting-IPrequest.ip
- Session support via in-memory data store
- Alternatively, specify a data store
- Cookie parsing support, including signed cookies
- Injects a unique request ID for every single request both in the log and in the response as
X-Request-IDandrequest.id - Injects the amount of time elapsed between when the request is received and when the response headers are written in
X-Response-Timewhich is also logged in the log entry. - Simple support for "protected" routes that allow for specifying a simple AuthenticationProvider to "protect" those routes
- WebSocket via additional method signature of
.ws('path', (socket, request) => void) - Simple cloudflared support to spin up test tunnels using Cloudflare
- Note: The public URLs will be randomly generated by Cloudflare
Documentation
https://gibme-npm.github.io/webserver/
Sample Code
import WebServer, { Logger } from '@gibme/webserver';
(async() => {
const app = WebServer({
autoStartCloudflared: true
});
app.get('/', (request, response) => {
return response.json({ success: true });
})
app.ws('/wss', (socket) => {
socket.on('message', msg => {
// simply echo the message back
socket.send(msg);
});
});
await app.start();
Logger.info('Listening on: %s', app.localUrl);
Logger.info('Listening on: %s', app.tunnelUrl);
Logger.info('Listening on: %s', app.url);
})();