express-wrapper-simple
v1.5.4
Published
Express wrapper with quick custom request and response handling
Downloads
18
Maintainers
Readme
Express Wrapper Simple
Simple express wrapper to provide standard formatted request and response handling. Sample provided in typescript.
Installation
npm i express-wrapper-simple
Quick 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
express
application auto include middlewares:- body-parser (express built-in)
- helmet
- cors
- compression
- morgan
expressWrapper
- extends express
request
,response
,nextFunction
toreq
,resp
,next
resp
implementssendSuccess
andsendError
function- express response wrap with default
success
orerror
object
- 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
resp
implements:sendSuccess
(200)sendError
badrequest
(400)unauthorized
(401)forbidden
(403)notfound
(404)toomanyrequest
(429)unknown
(500)maintenance
(500)
express response wrap with default success and error object
success
objectsuccess
(boolean) = truemessage
(string)data
(any)paging
(any)
error
objectsuccess
(boolean) = falsemessage
(string)error
(any)code
(string)maintenance
(boolean)
req
handles:query
limit
(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)limit
(int) - default: 15sortDir
(string) - default: ascsortBy
(string) - default: _idpagingMode
(string) - default: nopagingpage
(int) - default: 1sort
(list of [sortBy]: [sortDir], key/value pairs)nextToken
(string)