1.0.14 • Published 2 years ago
@ndiinginc/proxy v1.0.14
Install
npm install @ndiinginc/proxy
EventEmitter
Constructor
Methods
Logger
Properties
Methods
Server
Properties
Static Methods
ProxyServer
Methods
Examples
// Create proxy
const proxy1 = new ProxyServer()
// Network monitoring/logging
// (eventName, requestObject)
proxy1.logging.on('request.*', console.log)
// (eventName, responseObject)
proxy1.logging.on('response.*', console.log)
// (eventName, logObject)
// logObject={id,request,response}
proxy1.logging.on('update.*', console.log)
// Start server
proxy1.listen(8888, () => {
console.log('proxy1 started')
})
// Stop server after
setTimeout(() => {
proxy1.close()
console.log('proxy1 stopped')
}, 1000 * 5)
// Create anaother proxy
// it will subscribe with previous active proxy
const proxy2 = new ProxyServer()
// Intercept request and response
// using `use` function for global `request.method`
// and post,get,patch,put,delete for spesific `request.method`
// it's not like router middleware
proxy2.use('https://jsonplaceholder.typicode.com/posts', (req,res,next) => {
// this callback will call twice
// on before request
if(req){
// intercept req
delete req.headers['if-none-match'] // remove cache
}
// and before response
if(res){
// intercept res
res.body = JSON.stringify([]) // response with empty json array
}
// then continue request/response
// with `next` callback method
next()
})
// Start server
// when previous proxy still active
// this method never been call
// so the port are still the saame
proxy2.listen(8888, () => {
console.log('proxy2 started')
})
// Stop server after
// also it never been stop until all proxy are stopped
setTimeout(() => {
proxy2.close()
console.log('proxy2 stopped')
}, 1000 * 10)
// and thats it, just simple like that