Browse Source

added payload middleware

master
mortie 7 years ago
parent
commit
d41137fa6f
6 changed files with 50 additions and 4 deletions
  1. 3
    1
      README.md
  2. 6
    1
      example.js
  3. 12
    0
      index.js
  4. 23
    0
      js/middleware.js
  5. 1
    1
      package.json
  6. 5
    1
      web/index.html

+ 3
- 1
README.md View File



* `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



+ 6
- 1
example.js View File

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).

+ 12
- 0
index.js View File

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);

+ 23
- 0
js/middleware.js View File

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;

+ 1
- 1
package.json View File

{ {
"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": {

+ 5
- 1
web/index.html View File

<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>

Loading…
Cancel
Save