Selaa lähdekoodia

added payload middleware

master
mortie 7 vuotta sitten
vanhempi
commit
d41137fa6f
6 muutettua tiedostoa jossa 50 lisäystä ja 4 poistoa
  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 Näytä tiedosto

@@ -87,8 +87,10 @@ In the future, it will be possible to control the logging level.

* `webframe.middleware.cookies`: Adds `request.cookies`, parsed COOKIE header.
* `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


+ 6
- 1
example.js Näytä tiedosto

@@ -5,10 +5,15 @@ var mw = webframe.middleware;
var app = new webframe.App();

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

// 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
// regular expression pattern. "^.*" matches everything (though endpoints
// with explicit paths, like the "/endpoint" above, are prioritized).

+ 12
- 0
index.js Näytä tiedosto

@@ -137,6 +137,18 @@ class App {
if (typeof func !== "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
if (path[0] === "^") {
var pat = new RegExp(path);

+ 23
- 0
js/middleware.js Näytä tiedosto

@@ -33,5 +33,28 @@ function params(req, res, 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.params = params;
exports.payload = payload;

+ 1
- 1
package.json Näytä tiedosto

@@ -1,6 +1,6 @@
{
"name": "webframe",
"version": "0.2.1",
"version": "0.3.0",
"description": "Web server.",
"main": "index.js",
"scripts": {

+ 5
- 1
web/index.html Näytä tiedosto

@@ -7,8 +7,12 @@
<body>
<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>
</form>

<form action="/payload-example" method="post">
<label>Payload: <input type="text" name="payload"></label>
</form>
</body>
</html>

Loading…
Peruuta
Tallenna