1.5.4 • Published 4 years ago
express-wrapper-simple v1.5.4
Express Wrapper Simple
Simple express wrapper to provide standard formatted request and response handling. Sample provided in typescript.
Installation
npm i express-wrapper-simpleQuick Start
- Import express wrapper as main express library
import { Express } from 'express-wrapper-simple';
...
// use express-wrapper-simple Express app:
// include default middlewares - body-parser (express built-in), helmet, cors, compression, morgan (log)
// include expressWrapper middleware
// include expressPagingWrapper middleware
this.express = Express({
log: {
enable: true,
maxFileSize: '50M',
interval: '1d'
},
cors: {
methods: ['OPTIONS', 'GET', 'PUT', 'PATCH', 'POST', 'DELETE'],
exposedHeaders: ['X-Auth-Token']
},
bodyParser: {
// optionsJson: {
// strict: true
// },
optionsUrlencoded: {
extended: false
},
},
});
...
// use imported express normally
this.express.use(otherMiddleWare());- Use as express wrapper as middleware
import * as express from 'express';
import { expressWrapper, expressPagingWrapper } from 'express-wrapper-simple';
...
// use default Express app
public express: express.Application = express();
// add expressWrapper middleware - only include express request(req) and response(res) wrapping
this.express.use(expressWrapper);
// add expressPagingWrapper middleware - only include request paging wrapping
this.express.use(expressPagingWrapper);
// use express normally
this.express.use(otherMiddleWare());Features
expressapplication auto include middlewares:- body-parser (express built-in)
- helmet
- cors
- compression
- morgan
expressWrapper- extends express
request,response,nextFunctiontoreq,resp,next respimplementssendSuccessandsendErrorfunction- express response wrap with default
successorerrorobject
- extends express
expressPagingWrapper- auto convert
query.*intoquery.paging.*for easier pagination handling
- auto convert
Examples (expressWrapper)
- Using
ExpressRouter,Req,Resp,response
import { ExpressRouter, Req, Resp, response } from 'express-wrapper-simple';
router: ExpressRouter = ExpressRouter();
...
//
// use built-in 'res.sendSuccess' & 'res.sendError' function
//
this.router.use('/login', (req: Req, res: Resp) => {
if (!req.body.username) return res.sendError.badRequest('missing body.username');
if (!req.body.password) return res.sendError.badRequest('missing body.password');
//
// login handling logic
//
if (unauthorized) return res.sendError.unauthorized('invalid username / password', errorDetail);
return res.sendSuccess('login successfully', userDetails);
});
//
// different way of 'response.success' & 'response.error' usage
//
this.router.use('/another-login',(req: Req, res: Resp) => {
if (!req.body.token) return res.status(403).json(response.error('Unauthorized', errorDetails, 'login-error-code'));
//
// login handling logic
//
return res.status(200).json(response.success('Welcome', userData));
});
...
this.expressApp.use('/api/auth', this.router);References
respimplements:sendSuccess(200)sendErrorbadrequest(400)unauthorized(401)forbidden(403)notfound(404)toomanyrequest(429)unknown(500)maintenance(500)
express response wrap with default success and error object
successobjectsuccess(boolean) = truemessage(string)data(any)paging(any)
errorobjectsuccess(boolean) = falsemessage(string)error(any)code(string)maintenance(boolean)
reqhandles:querylimit(int) - max limit to 500sortDir(string) - value: 'asc', 'desc'sortBy(string)pagingMode(string)page(int)nextToken(string)
- NEW
query.paging(convert query above into query.paging)
1.5.4
4 years ago
1.5.3
4 years ago
1.5.2
4 years ago
1.5.1
4 years ago
1.5.0
4 years ago
1.4.5
5 years ago
1.4.4
5 years ago
1.4.3
5 years ago
1.4.2
5 years ago
1.4.1
5 years ago
1.4.0
5 years ago
1.3.1
5 years ago
1.3.0
5 years ago
1.2.4
5 years ago
1.2.3
5 years ago
1.2.2
5 years ago
1.2.0
5 years ago
1.2.1
5 years ago
1.1.12
5 years ago
1.1.11
5 years ago
1.1.10
5 years ago
1.1.9
5 years ago
1.1.8
5 years ago
1.1.7
5 years ago
1.1.6
5 years ago
1.1.5
5 years ago
1.1.4
5 years ago
1.1.3
5 years ago
1.1.2
5 years ago
1.1.1
5 years ago
1.1.0
5 years ago
1.0.2
5 years ago
1.0.1
5 years ago
1.0.0
5 years ago