* `webframe.middleware.cookies`: Adds `request.cookies`, parsed COOKIE header. | * `webframe.middleware.cookies`: Adds `request.cookies`, parsed COOKIE header. | ||||
* `webframe.middleware.params`: Adds `request.params`, parsed URL parameters. | * `webframe.middleware.params`: Adds `request.params`, parsed URL parameters. | ||||
* `webframe.middleware.payload`: Adds `request.payload`, a string (max 50 | |||||
kilobytes) of the request payload. | |||||
In the future, there will be middleware for POST request too. | |||||
In the future, there will be middleware for form data over POST too. | |||||
### Modifications to the request and response objects | ### Modifications to the request and response objects | ||||
var app = new webframe.App(); | var app = new webframe.App(); | ||||
// Endpoint which uses the "params" middleware to parse URL parameters | // Endpoint which uses the "params" middleware to parse URL parameters | ||||
app.get("/endpoint", [ mw.params ], (req, res) => { | |||||
app.get("/params-example", [ mw.params ], (req, res) => { | |||||
res.end("Hello, "+req.params.name); | res.end("Hello, "+req.params.name); | ||||
}); | }); | ||||
// Endpoint which uses the "payload" middleware to get the payload as a string | |||||
app.post("/payload-example", [ mw.payload ], (req, res) => { | |||||
res.end(req.payload); | |||||
}); | |||||
// When a an endpoint path starts with a ^, it's interpreted as a | // When a an endpoint path starts with a ^, it's interpreted as a | ||||
// regular expression pattern. "^.*" matches everything (though endpoints | // regular expression pattern. "^.*" matches everything (though endpoints | ||||
// with explicit paths, like the "/endpoint" above, are prioritized). | // with explicit paths, like the "/endpoint" above, are prioritized). |
if (typeof func !== "function") | if (typeof func !== "function") | ||||
throw new TypeError("Func must be a function."); | throw new TypeError("Func must be a function."); | ||||
// All middlewares must exist | |||||
if (middleware) { | |||||
for (var i in middleware) { | |||||
if (typeof middleware[i] !== "function") { | |||||
this.panic( | |||||
"Middleware "+i+" for "+method+" "+path+ | |||||
" is "+(typeof middleware[i])+", expected function."); | |||||
return; | |||||
} | |||||
} | |||||
} | |||||
// Add to routes array or route map | // Add to routes array or route map | ||||
if (path[0] === "^") { | if (path[0] === "^") { | ||||
var pat = new RegExp(path); | var pat = new RegExp(path); |
cb(); | cb(); | ||||
} | } | ||||
/* | |||||
* Add req.payload | |||||
*/ | |||||
function payload(req, res, cb, app) { | |||||
var max = 50 * 1000; | |||||
req.payload = ""; | |||||
var warned = false; | |||||
req.on("data", d => { | |||||
if (!warned && req.payload.length + d.length > max) { | |||||
app.warning("Payload with length above "+max+" bytes ignored."); | |||||
return cb(); | |||||
} | |||||
req.payload += d; | |||||
}); | |||||
req.on("end", () => { | |||||
if (!warned) | |||||
cb(); | |||||
}); | |||||
} | |||||
exports.cookies = cookies; | exports.cookies = cookies; | ||||
exports.params = params; | exports.params = params; | ||||
exports.payload = payload; |
{ | { | ||||
"name": "webframe", | "name": "webframe", | ||||
"version": "0.2.1", | |||||
"version": "0.3.0", | |||||
"description": "Web server.", | "description": "Web server.", | ||||
"main": "index.js", | "main": "index.js", | ||||
"scripts": { | "scripts": { |
<body> | <body> | ||||
<p> Hello World! </p> | <p> Hello World! </p> | ||||
<form action="/endpoint" method="get"> | |||||
<form action="/params-example" method="get"> | |||||
<label>What's your name? <input type="text" name="name"></label> | <label>What's your name? <input type="text" name="name"></label> | ||||
</form> | </form> | ||||
<form action="/payload-example" method="post"> | |||||
<label>Payload: <input type="text" name="payload"></label> | |||||
</form> | |||||
</body> | </body> | ||||
</html> | </html> |