0.0.12 • Published 25 days ago
http-mw v0.0.12
http-mw
Bare minimum middleware for node's built-in http server module
Features
- Parses incoming form data on each POST request, supports modern payload types.
- Parses query parameters if exists
- Accepts multiple file uploads.
- Built-in static web server.
- Basic http security, filters out the abusive clients.
- Session support, assigns a persistent id to every client.
- Automatic mime type detection.
- CORS friendly.
Installation
$ npm i http-mw
Usage
const server = require("http-mw");
server.listen({
"port": 8080,
"webroot": "./public",
"uploads": "./files"
}, function (req) {
switch (req.method) {
case "get":
switch (req.path[0]) {
// api endpoint
case "info":
req.send({
"ip": req.ip,
"sid": req.sid,
"params": req.params,
"cookies": req.cookies
});
break;
// string type detection test
case "test":
req.send('<html><body><h1>It Works!</h1></body></html>');
break;
// redirect test
case "redirect":
req.redirect("http://google.com/");
break;
// respond with error
case "error":
req.deny("You don't have access to this resource", 401);
break;
default:
// serve static files from webroot dir
req.static();
}
break;
case "post":
switch (req.path[0]) {
case "info":
req.send({
"ip": req.ip,
"sid": req.sid,
"payload": req.payload,
"files": req.files
});
break;
default:
req.deny("undefined endpoint", 404);
}
}
}, function (conf) {
console.log("-- server running on port " + conf.port);
console.log("-- middleware test url: http://localhost:" + conf.port + "/info");
});
Documentation
Not available yet