@reignmodule/express-utils
v2.0.1
Published
Common services express utilities
Downloads
9
Readme
express services utilities
Common express services utilities
listeningListener()
listeningListener(this: ServerHttp): void
Use it to show a listener server.
Sample:
import { listeningListener } from '@reignmodule/express-utils/listening-listener';
createServerHttp(app).listen(3000, listeningListener);
// Server ready on http://localhost:3000
createServerHttps(app).listen(3001, listeningListener);
// Server ready on https://localhost:3001
createServerHttp2(app).listen(3002, listeningListener);
// Server ready on //localhost:3002
requestHandlerAsync
requestHandlerAsync(requestHandler: (req: Request, res: Response, next: NextFunction) => Promise<any>, thisArg?: any) => RequestHandler
Sample
import { requestHandlerAsync } from '@reignmodule/express-utils/handle-async';
const app = express();
app.get('/', requestHandlerAsync(async (req, res) => {
res.send('ok');
}));
errorRequestHandlerAsync
errorRequestHandlerAsync(errorRequestHandler: (error: any, req: Request, res: Response, next: NextFunction) => Promise<any>, thisArg?: any) => ErrorRequestHandler
Sample
import { errorRequestHandlerAsync } from '@reignmodule/express-utils/handle-async';
const app = express();
app.get('/', errorRequestHandlerAsync(async (err, req, res) => {
res.status(500).send(err.message);
}));
## requestParamHandlerAsync
requestParamHandlerAsync(requestParamHandler: (req: Request, res: Response, next: NextFunction, value: any, name: string) => Promise<any>, thisArg?: any) => RequestParamHandler
Sample
import { requestParamHandlerAsync } from '@reignmodule/express-utils/handle-async';
const app = express();
app.param('user', requestParamHandlerAsync(async (req, res, next, id, param) => {
const user = await User.find(id);
req.user = user;
return next();
}));