mirror of
https://github.com/Jermolene/TiddlyWiki5
synced 2024-06-28 16:23:15 +00:00
Allow all methods and add bodyFormat property to route definition (#3362)
* Allow all methods and add bodyFormat property to route definition * Set string as the default bodyFormat * Only set encoding on string routes
This commit is contained in:
parent
8b787cd806
commit
aa8b2e11bb
|
@ -196,16 +196,14 @@ Server.prototype.requestHandler = function(request,response) {
|
||||||
response.end();
|
response.end();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// Set the encoding for the incoming request
|
|
||||||
// TODO: Presumably this would need tweaking if we supported PUTting binary tiddlers
|
//receive the request body if necessary and hand off to the route handler
|
||||||
request.setEncoding("utf8");
|
if(route.bodyFormat === "stream" || request.method === "GET" || request.method === "HEAD"){
|
||||||
// Dispatch the appropriate method
|
//let the route handle the request stream itself
|
||||||
switch(request.method) {
|
|
||||||
case "GET": // Intentional fall-through
|
|
||||||
case "DELETE":
|
|
||||||
route.handler(request,response,state);
|
route.handler(request,response,state);
|
||||||
break;
|
} else if(route.bodyFormat === "string" || !route.bodyFormat){
|
||||||
case "PUT":
|
// Set the encoding for the incoming request
|
||||||
|
request.setEncoding("utf8");
|
||||||
var data = "";
|
var data = "";
|
||||||
request.on("data",function(chunk) {
|
request.on("data",function(chunk) {
|
||||||
data += chunk.toString();
|
data += chunk.toString();
|
||||||
|
@ -214,7 +212,17 @@ Server.prototype.requestHandler = function(request,response) {
|
||||||
state.data = data;
|
state.data = data;
|
||||||
route.handler(request,response,state);
|
route.handler(request,response,state);
|
||||||
});
|
});
|
||||||
break;
|
} else if(route.bodyFormat === "buffer"){
|
||||||
|
var data = [];
|
||||||
|
request.on("data",function(chunk) {
|
||||||
|
data.push(chunk);
|
||||||
|
});
|
||||||
|
request.on("end",function(){
|
||||||
|
state.data = Buffer.concat(data);
|
||||||
|
route.handler(request,response,state);
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
throw "Invalid bodyFormat " + route.bodyFormat + " in route " + route.method + " " + route.path.source;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user