const requestIp = require('request-ip') module.exports = function (req, res, next) { // Website you wish to allow to connect res.setHeader('Access-Control-Allow-Origin', '*') // Request methods you wish to allow res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE') // Request headers you wish to allow res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type,token,authorization') // req.clientIp = requestIp.getClientIp(req) // res.done = (pStatus, pErr, pJson) => { let status = null let error = null let json = null if (typeof pStatus === 'number') { status = pStatus error = pErr json = pJson || {} } else { error = pStatus json = pErr || {} } if (!error && status >= 400) { error = 'Error:401' } if (error) { status = status || 555 let errorToSend = { error: '', httpStatus: status, stack: null, more: json || {} } if (!(error instanceof Error)) { if (typeof error === 'object') error = JSON.stringify(error) error = new Error(error) } errorToSend.error = error.message errorToSend.stack = error.stack res.status(status).json(errorToSend) } else { res.json(json) } } next() }