slower
v2.2.0
Published
A package for simple HTTP server routing.
Readme
Slower
A minimal, dependency-free HTTP framework for Node.js.
Slower provides a small subset of Express.js–style routing and middleware with a simpler internal model.
Don't want to download 200 megabytes of modules from Express? Then try Slower 😉.
It's just a couple files and a few KB.
This might not be the most robust library, but it is small and lightweight, and it does the job. (Just don't use this in production, please.)
A word about this:
Sorry for the (really) poorly written documentation, I need time to re-write it. If you would like to help, just email me (email on package.json), and I will be more than happy to talk to you.
Features
- No external dependencies
- Express-like API and functionality (
get,post,use,all, etc.) - Static file serving
- Sub-routers and route grouping
- SMALL!
Installation
npm install slower(Or just download this source code and put on a 'lib/slower' folder, it's what I usually do).
Basic Usage
Check examples/ for some samples on how to use this library.
The basic is: if works on Express, probably works here too (apart from some minor modifications, there is very little difference in the two public APIs)
const slower = require('slower');
const app = slower();
app.get('/', (req, res) => {
res.send('Hello world');
});
app.listen(3000);Template URLs:
The same basic rules that express.js applied are available here, since JS internalized the URLPattern API, and this library uses the pattern matching from URLPattern.
If you have questions, check this guide on URLPattern: https://developer.mozilla.org/en-US/docs/Web/API/URL_Pattern_API#pattern_syntax
Routing
HTTP Methods
All standard HTTP verbs are supported:
app.get('/users', handler);
app.post('/users', handler);
app.put('/users/:id', handler);
app.delete('/users/:id', handler);Route Parameters
Routes use URLPattern internally and support named parameters:
app.get('/users/:id', (req, res) => {
res.json({ id: req.params.id });
});Multiple Handlers / Middleware
Handlers are executed sequentially and receive next():
app.get('/secure', authMiddleware, (req, res) => {
res.send('Authorized');
});Middleware
use()
Attach middleware for all HTTP methods:
app.use((req, res, next) => {
console.log(req.method, req.url);
next();
});all()
Apply handlers to specific paths or methods:
app.all('/health', (req, res) => {
res.send('OK');
});Request Object (req)
- req.params – route parameters
- req.query – parsed query string
- req.body
- buffer
- text()
- json()
- req.ip – remote IP address
- req.session
- host
- port
- rhost - remote connected host
- rport - remote connected port
Example:
app.post('/data', async (req, res) => {
const body = req.body.json();
res.json(body);
});Response Object (res)
- res.status(code)
- res.set(name, value)
- res.get(name)
- res.type(mimeOrExtension)
- res.send(body)
- res.json(object)
- res.file(path)
- res.render(templatePath, data)
- res.redirect(path)
Example:
res.status(201).json({ success: true });Static Files
Serve a directory of static files:
app.static('public');Or mount it at a specific path:
app.static('public', '/assets');HTML files are also served without the .html extension automatically.
Routers
Sub-Router
const router = slower.Router();
router.get('/users', handler);
router.post('/users', handler);
app.useRouter(router, '/api');Route Groups
app.route('/users').get(listUsers).post(createUser);HTTPS Support
Pass options directly to the underlying server:
const fs = require('fs');
const app = slower({
https: true,
key: fs.readFileSync('key.pem'),
cert: fs.readFileSync('cert.pem'),
});Fallback page
If no route matches, it returns a default 404 HTML response:
Cannot {METHOD} /{path}
Custom fallback behavior can be implemented using middleware.
Method documentation
Main Export
slower(options?: object) : <SlowerRouter>Creates a new Slower application instance.
optionsare forwarded tohttp.createServerorhttps.createServer- If
options.https === true, HTTPS is used
Class: <SlowerRouter>
Main application router and HTTP server wrapper.
Server Control
Start the HTTP/HTTPS server:
<SlowerRouter>.listen(...args) : http.ServerStops the server:
<SlowerRouter>.close(callback?: function) : voidRouting Methods
The following methods share the same signature:
<SlowerRouter>.get(path, ...handlers) : <SlowerRouter>
<SlowerRouter>.post(path, ...handlers) : <SlowerRouter>
<SlowerRouter>.put(path, ...handlers) : <SlowerRouter>
<SlowerRouter>.delete(path, ...handlers) : <SlowerRouter>
<SlowerRouter>.patch(path, ...handlers) : <SlowerRouter>
<SlowerRouter>.options(path, ...handlers) : <SlowerRouter>
<SlowerRouter>.head(path, ...handlers) : <SlowerRouter>
<SlowerRouter>.trace(path, ...handlers) : <SlowerRouter>
<SlowerRouter>.connect(path, ...handlers) : <SlowerRouter>pathmay be a string, RegExp, or middleware function- Handlers are called sequentially with
(req, res, next)
Middleware
<SlowerRouter>.use(...handlers: function\[\]) : <SlowerRouter>Registers global middleware for all HTTP methods.
<SlowerRouter>.all(pathOrMethod, ...handlers) : <SlowerRouter>Registers handlers across multiple HTTP methods depending on the first argument.
Static Files
<SlowerRouter>.static(directoryPath: string, mountPath?: string) : <SlowerRouter>Serves static files from a directory.
Routers
<SlowerRouter>.useRouter(router: <SlowerSubRouter>, mountPath?: string) : <SlowerRouter>Mounts a sub-router at the specified path.
<SlowerRouter>.route(path: string | RegExp) : <SlowerSubRouter>Creates a route group bound to a single path.
Class: <\SlowerSubRouter>
Router container used for grouping routes before mounting.
<SlowerSubRouter>.get(path, ...handlers) : <SlowerSubRouter>
<SlowerSubRouter>.post(path, ...handlers) : <SlowerSubRouter>
<SlowerSubRouter>.put(path, ...handlers) : <SlowerSubRouter>
<SlowerSubRouter>.delete(path, ...handlers) : <SlowerSubRouter>
<SlowerSubRouter>.patch(path, ...handlers) : <SlowerSubRouter>
<SlowerSubRouter>.use(...handlers) : <SlowerSubRouter>
<SlowerSubRouter>.all(pathOrMethod, ...handlers) : <SlowerSubRouter>
<SlowerSubRouter>.static(directoryPath, mountPath?) : <SlowerSubRouter>
<SlowerSubRouter>.route(path) : <SlowerMicroRouter>Request Extensions
req.get(headerName: string) : string | undefined
req.body.buffer : Buffer
req.body.text() : string
req.body.json() : any
req.query : object
req.params : object | undefined
req.ip : string
req.session : objectResponse Extensions
res.status(code: number) : res
res.set(name: string | object, value?: string) : res
res.get(name: string) : string | undefined
res.type(type: string) : res
res.send(body: string | Buffer) : void
res.json(data: any) : void
res.file(path: string) : void
res.render(templatePath: string, data?: object) : void
res.redirect(path: string) : voidLicense: MIT
License
This is MIT-licensed. Just use it.
