Middleware
Middleware is a function to the next handlers.
import { nhttp, Handler } from "@nhttp/nhttp";
const app = nhttp();
const foo: Handler = (rev, next) => {
rev.foo = "foo";
return next();
}
app.use(foo);
app.get("/foo", ({ foo }) => foo);
app.listen(3000);
Express middleware support
import { nhttp, Handler, HttpError } from "@nhttp/nhttp";
import { body, validationResult } from "npm:express-validator";
const app = nhttp();
// example express validator
const validator: Handler[] = [
body("username").isString(),
body("password").isLength({ min: 6 }),
body("email").isEmail(),
(rev, next) => {
const errors = validationResult(rev);
if (!errors.isEmpty()) {
throw new HttpError(422, errors.array());
}
return next();
},
];
app.post("/user", validator, ({ body }) => body);
app.listen(3000);