Преглед на файлове

added payload middleware

master
mortie преди 7 години
родител
ревизия
d41137fa6f
променени са 6 файла, в които са добавени 50 реда и са изтрити 4 реда
  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 Целия файл

@@ -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 Целия файл

@@ -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 Целия файл

@@ -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 Целия файл

@@ -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 Целия файл

@@ -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 Целия файл

@@ -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…
Отказ
Запис