combo-url-parser
Advanced tools
Comparing version 0.1.2 to 0.1.3
var parser = require('./parser.js'); | ||
var request = require('request'); | ||
var CombinedStream = require('combined-stream'); | ||
var mime = require('mime'); | ||
var async = require('async'); | ||
/** | ||
@@ -11,24 +11,73 @@ * Middleware for connect | ||
module.exports = function () { | ||
return function (req, res, next) { | ||
var urls = parser(req.url); | ||
if (!urls) { | ||
next(); | ||
return; | ||
} | ||
var headers = ['content-type', 'vary'].map(function (key) { | ||
return [key, key.split('-').map(function (word) { | ||
return word[0].toUpperCase() + word.substr(1) | ||
}).join('-')] | ||
}); | ||
// http://stackoverflow.com/questions/7109732/express-setting-content-type-based-on-path-file | ||
var type = mime.lookup(urls[0]); | ||
return function (req, res, next) { | ||
var urls = parser(req.url); | ||
if (!res.getHeader('content-type')) { | ||
res.setHeader('Content-Type', type + '; charset=UTF-8'); | ||
if (!urls) { | ||
next(); | ||
return; | ||
} | ||
async.mapLimit(urls, 8, function (url, cb) { | ||
var fullURL = 'http://' + req.headers.host + url; | ||
request({ | ||
url: fullURL, | ||
encoding: null | ||
}, function (err, resp, body) { | ||
if (err) { | ||
cb(err); | ||
return; | ||
} | ||
if (resp.statusCode === 404) { | ||
var error = new Error('404'); | ||
error.url = fullURL; | ||
cb(error); | ||
} | ||
cb(null, {response: resp, body: body}); | ||
}); | ||
}, function (err, results) { | ||
if (err) { | ||
if (err.message === '404') { | ||
err.statsCde = 404; | ||
res.end('Sorry, Can not found '+ err.url); | ||
} else { | ||
next(err); | ||
} | ||
return; | ||
} | ||
var response = results[0].response; | ||
headers.forEach(function (key) { | ||
var v = response.headers[key[0]]; | ||
if (v) { | ||
res.setHeader(key[1], v); | ||
} | ||
}); | ||
res.setHeader('Access-Control-Allow-Origin', '*'); | ||
var newline = new Buffer('\n'); | ||
res.end(Buffer.concat(results.reduce(function(all, result){ | ||
if (all.length) { | ||
all.push(newline); | ||
} | ||
all.push(result.body); | ||
return all; | ||
}, []))); | ||
}); | ||
} | ||
var combinedStream = CombinedStream.create(); | ||
urls.forEach(function (path) { | ||
combinedStream.append(request('http://' + req.headers.host + path)); | ||
combinedStream.append('\n'); | ||
}); | ||
combinedStream.pipe(res); | ||
} | ||
}; |
@@ -5,3 +5,3 @@ { | ||
"description": "A util to parser comboed url", | ||
"version": "0.1.2", | ||
"version": "0.1.3", | ||
"repository": { | ||
@@ -18,5 +18,4 @@ "type": "git", | ||
"dependencies": { | ||
"combined-stream": "0.0.4", | ||
"request": "~2.34.0", | ||
"mime": "~1.2.11" | ||
"async": "~0.7.0" | ||
}, | ||
@@ -23,0 +22,0 @@ "devDependencies": { |
@@ -27,3 +27,9 @@ [![build status](https://secure.travis-ci.org/maxbbn/combo-url-parser.png)](http://travis-ci.org/maxbbn/combo-url-parser) | ||
### 0.1.3 | ||
- 中间件,去掉配置,现在不需要配置 port | ||
- 优化逻辑,如果一个文件不存在,就返回 404, 方便问题定件 | ||
- 使用第一个请求的 mime-type | ||
### 0.1.2 | ||
- 添加 content-type Header | ||
@@ -30,0 +36,0 @@ |
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
5733
2
122
39
+ Addedasync@~0.7.0
+ Addedasync@0.7.0(transitive)
+ Addedcombined-stream@0.0.7(transitive)
- Removedcombined-stream@0.0.4
- Removedmime@~1.2.11
- Removedcombined-stream@0.0.4(transitive)