Add convenience function to the req and res objects of an http/https server
npm install easyreqeasyreq
=======
Add convenience function to the req and res objects of an http/https server
Install
-------
npm install easyreq
Example
-------
Use easyreq to decorate the req and res objects of a server
`` js
var http = require('http');
var easyreq = require('easyreq');
http.createServer(function(req, res) {
easyreq(req, res); // call easyreq to overload these object with new methods
console.log(req.urlparsed); // the output of url.parse(req.url, true);
res.error(); // end the connection with a 500
res.notfound(); // end the connection with a 404
res.redirect('http://www.google.com'); // end the connection with a 302 redirect to google
});
`
The output of url.parse(req.url, true). Additional keys are also available:
- req.urlparsed.normalizedpathname: the output ofpath.normalize(req.urlparsed.pathname)
; used to help prevent directoryreq.urlparsed.query
traversal attacks against your webserver.
- : this object has been
cleansed of reserved
keywords
End the connection with a given code which defaults to 500. This allows for
simple one-liners like:
` js`
if (err)
return res.error();
The second argument is an optional string to pass to res.end(), if it is not supplied,http.STATUS\_CODES[code]
the data will be set to
Examples
` js
// send a 500 with the body set to "Internal Server Error" (http.STATUS_CODES[500])
res.error();
// send a 501 with the body set to "Not Implemented" (http.STATUS_CODES[501])
res.error(501);
// send a 500 with the body set to "foo"
res.error('foo');
// send a 503 with the body set to "bar"
res.error(503, 'bar');
`
Like res.error() above, this will end the connection with a 404. Again this
allows for simple one-liners like:
` js`
if (!route)
return res.notfound();
Any arguments passed to res.notfound() will be applied to the res.end()
function.
Send a 302 redirect to the given URL and end the connection.
The optional second argument is the code to send, defaults to 302
End the request by sending an object as JSON. obj is automatically stringified,Content-Type
the header is set if it is not currently set.
Code is optional and defaults to 200
Note: this function can throw if the object cannot be stringified
End the request by sending HTML. Content-Type header is set if it is
not currently set.
Code` is optional and defaults to 200
License
-------
MIT