A package for simple HTTP server routing.
npm install slowerA 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.
---
- No external dependencies
- Express-like API and functionality (get, post, use, all, etc.)
- Static file serving
- Sub-routers and route grouping
- SMALL!
---
``sh`
npm install slower
(Or just download this source code and put on a 'lib/slower' folder, it's what I usually do).
---
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)
`js
const slower = require('slower');
const app = slower();
app.get('/', (req, res) => {
res.send('Hello world');
});
app.listen(3000);
`
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
#### HTTP Methods
All standard HTTP verbs are supported:
`js`
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:
`js`
app.get('/users/:id', (req, res) => {
res.json({ id: req.params.id });
});
#### Multiple Handlers / Middleware
Handlers are executed sequentially and receive next():
`js`
app.get('/secure', authMiddleware, (req, res) => {
res.send('Authorized');
});
#### Middleware
use()
Attach middleware for all HTTP methods:
`js`
app.use((req, res, next) => {
console.log(req.method, req.url);
next();
});
all()
Apply handlers to specific paths or methods:
`js`
app.all('/health', (req, res) => {
res.send('OK');
});
- 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:
`js`
app.post('/data', async (req, res) => {
const body = req.body.json();
res.json(body);
});
- 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:
`js`
res.status(201).json({ success: true });
Serve a directory of static files:
`js`
app.static('public');
Or mount it at a specific path:
`js`
app.static('public', '/assets');
HTML files are also served without the .html extension automatically.
#### Sub-Router
`js
const router = slower.Router();
router.get('/users', handler);
router.post('/users', handler);
app.useRouter(router, '/api');
`
#### Route Groups
`js`
app.route('/users').get(listUsers).post(createUser);
Pass options directly to the underlying server:
`js
const fs = require('fs');
const app = slower({
https: true,
key: fs.readFileSync('key.pem'),
cert: fs.readFileSync('cert.pem'),
});
`
If no route matches, it returns a default 404 HTML response:
Cannot {METHOD} /{path}
Custom fallback behavior can be implemented using middleware.
`js`
slower(options?: object) :
Creates a new Slower application instance.
- options are forwarded to http.createServer or https.createServeroptions.https === true
- If , HTTPS is used
---
Main application router and HTTP server wrapper.
#### Server Control
Start the HTTP/HTTPS server:
``
Stops the server:
``
#### Routing Methods
The following methods share the same signature:
`
`
- path may be a string, RegExp, or middleware function(req, res, next)
- Handlers are called sequentially with
#### Middleware
``
Registers global middleware for all HTTP methods.
``
Registers handlers across multiple HTTP methods depending on the first argument.
#### Static Files
``
Serves static files from a directory.
#### Routers
``
Mounts a sub-router at the specified path.
``
Creates a route group bound to a single path.
Router container used for grouping routes before mounting.
`
`
`
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 : object
`
`
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) : void
``
License: MIT
This is MIT-licensed. Just use it.