$ npm install urest
const { Rest, JsonBodyParser } = require("urest");
const app = new Rest();
app.pre(JsonBodyParser.middleware());
app.get('/', (req, res) => res.send({message: 'Hello World'}));
app.post("/echo", (req, res) => res.send(req.body));
app.native().listen(8000);
exports.handle = (e, context) => app.lambda(e, context);
module.exports = app.gcf();
Errors passed into the next function will be logged, then returned to the client obscured behind a unique ID.
const { UErrors } = require("urest");
const { UInternalServerError } = UErrors;
app.get("/broken", (req, res, next) => next(new UInternalServerError("This is logged")));
// 500
{
"code":"InternalServer",
"eid":"3ccf6fadf79875f58631a8c7ecc302523b563423"
}
{
"level":500,
"request_id":"1a376e5eb266511a35aefcc7ffad7d50aef5df40",
"environment":"develop",
"service": "my-service",
"stack":"UInternalServerError: This is logged\n at Object.UError (urest/library/UErrors.js:7:8)\n at new <anonymous> (urest/library/UErrors.js:11:9)\n at runHandler (urest/library/Rest.js:79:22)\n at next (urest/library/Rest.js:87:4)\n at IncomingMessage.req.on.on (urest/library/JsonBodyParser.js:26:6)\n at emitNone (events.js:106:13)\n at IncomingMessage.emit (events.js:208:7)\n at endReadableNT (_stream_readable.js:1056:12)\n at _combinedTickCallback (internal/process/next_tick.js:138:11)\n at process._tickCallback (internal/process/next_tick.js:180:9)",
"message":"This is logged",
"eid":"3ccf6fadf79875f58631a8c7ecc302523b563423",
"code":"InternalServer",
"statusCode":500
}
Interceptors works in much the same way as middleware but act on the response before it is returned to the client.
The following example will check for the header "accept-encoding": "gzip"
and will compress response bodies as fit.
const { Rest, JsonBodyParser } = require("urest");
const { Neutron } = require("urequest");
const app = new Rest();
app.int(Neutron.intercept());
app.pre(JsonBodyParser.middleware());
app.post("/echo", (req, res) => res.send(req.body));
const server = app.native().listen(1234);
The value passed into res.send is attached as res.responseData
, the following example checks for a property in the response and prevents the request if not true.
const { Rest, UErrors } = require("urest");
const { UUnauthorizedError } = UErrors;
const app = new Rest();
app.int((req, res, next) => {
if (res.responseData.authed !== true) next(new UUnauthorizedError());
else next();
});