res-interceptor
Advanced tools
Comparing version 0.0.2 to 0.1.0
module.exports = require('./lib/interceptor'); |
@@ -1,7 +0,17 @@ | ||
var newData; | ||
var newHeader; | ||
function Interceptor(cb){ | ||
this.setBody=function(data){ | ||
this.body=function(data){ | ||
newData =data; | ||
}; | ||
this.set=function zzz(field, val){ | ||
if(arguments.length===2){ | ||
newHeader=JSON.parse('{"'+field.toString()+'":"'+val.toString()+'"}'); | ||
}else if(arguments.length===1){ | ||
if(typeof field!=='object') return; | ||
newHeader=field; | ||
} | ||
}; | ||
return function(req,res,next){ | ||
@@ -11,6 +21,6 @@ if(typeof cb !=='function') return next(); | ||
if(typeof req!=='object' || typeof res!=='object' ||typeof next !== 'function') return next(); | ||
newData=''; | ||
var responseObj = { | ||
headers:res._headers | ||
}; | ||
res.realSend=res.send; | ||
@@ -25,3 +35,8 @@ res.send=function(statusOrBody,body){ | ||
} | ||
cb(res,req,next,responseObj); | ||
cb(req,res,next,responseObj); | ||
if(typeof newHeader !== 'undefined') | ||
res.set(newHeader); | ||
if(newData===null || newData===''|| typeof newData==='undefined'){ | ||
@@ -39,2 +54,1 @@ return res.realSend(responseObj.body); | ||
module.exports=Interceptor; | ||
{ | ||
"name": "res-interceptor", | ||
"version": "0.0.2", | ||
"version": "0.1.0", | ||
"description": "Get response in middleware with Express.", | ||
@@ -21,9 +21,3 @@ "main": "./index.js", | ||
}, | ||
"homepage": "https://github.com/daysv/res-interceptor", | ||
"readme": "res-interceptor\r\n==================\r\n\r\nGet response in middleware with Express.\r\n\r\n##Installation\r\n\r\n```\r\nnpm install res-interceptor\r\n```\r\n\r\n##Usage\r\n\r\n```\r\nvar interceptor=require('res-interceptor');\r\n```\r\n```\r\nrouter.get('/',\r\n interceptor(function(res,req,next,data){\r\n console.log(data); // { headers: { 'x-powered-by': 'Express' }, status: 200, body: 'hello world' }\r\n this.setBody('rewrite it'); // rewrite response in middleware.\r\n })\r\n ,function(req,res){\r\n res.send('hello world');\r\n })\r\n;\r\n```\r\n\r\n##Warning\r\n\r\nDo not use `res.send()` in middleware! ", | ||
"readmeFilename": "README.md", | ||
"_id": "res-interceptor@0.0.2", | ||
"scripts": {}, | ||
"_shasum": "2777a034c2fe1e3ababfbd6f76c90d8d5dd259a3", | ||
"_from": "res-interceptor@" | ||
"homepage": "https://github.com/daysv/res-interceptor" | ||
} |
res-interceptor | ||
================== | ||
Get response in middleware with Express. | ||
Get & overwrite response in middleware with Express. | ||
@@ -9,3 +9,3 @@ ##Installation | ||
``` | ||
npm install res-interceptor | ||
npm install res-interceptor -save | ||
``` | ||
@@ -15,19 +15,28 @@ | ||
``` | ||
var interceptor=require('res-interceptor'); | ||
``` | ||
``` | ||
router.get('/', | ||
interceptor(function(res,req,next,data){ | ||
console.log(data); // { headers: { 'x-powered-by': 'Express' }, status: 200, body: 'hello world' } | ||
this.setBody('rewrite it'); // rewrite response in middleware. | ||
```js | ||
var express = require('express'); | ||
var interceptor=require('../interceptor'); | ||
var app = express(); | ||
app.use( | ||
interceptor (function (req,res,next,data) { | ||
console.log(data); // => { headers: { 'x-powered-by': 'Express', id: '1' }, status: 200, body: 'hello world' } | ||
this.set('id','2'); // overwrite response headers in middleware. | ||
// or | ||
this.set({ | ||
foo:'bar' | ||
}); | ||
this.body('Goodbye'); // overwrite response body in middleware. | ||
}) | ||
,function(req,res){ | ||
res.send('hello world'); | ||
}) | ||
; | ||
); | ||
app.get('/', function (req, res) { | ||
res.set('id','1'); | ||
res.send('hello world'); | ||
}); | ||
app.listen(3000); | ||
``` | ||
##Warning | ||
Do not use `res.send()` in middleware! | ||
##License | ||
###MIT |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
4482
6
63
41