2 Star 0 Fork 0

mirrors_opencollective/thecodebarbarian.com

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
server.js 2.13 KB
一键复制 编辑 原始数据 按行查看 历史
Valeri Karpov 提交于 2015-11-13 15:01 . fix up rss feed
var commander = require('commander');
var fs = require('fs');
var path = require('path');
commander.
option('-p, --port <port>', 'Port to run on', Number).
parse(process.argv);
require('http').createServer(function (request, response) {
request.addListener('end', function() {
console.log(request.url);
// Backwards-compatible support for wordpress, because wordpress allows
// URLs like `/2013/06/06/61/` to map to the blog post `61`
if (request.url.length > 1 &&
request.url.charAt(request.url.length - 1) === '/') {
request.url = request.url.substr(0, request.url.length - 1);
}
if (request.url.indexOf('?') !== -1) {
request.url = request.url.substr(0, request.url.indexOf('?'));
}
// Defend against directory traversals
var p = path.join('./bin', request.url.replace('../', ''));
fs.stat(p, function(err, stats) {
if (err || !stats) {
response.writeHead(404, {
'Cache-Control': 'max-age=7200',
'Content-Type': 'text/plain'
});
return response.end("Not found");
}
if (stats.isFile()) {
var type = path.extname(p);
if (type === '.xml') {
type = 'application/xml'
}
response.writeHead(200, {
'Cache-Control': 'max-age=7200',
'Content-Type': type || 'text/html'
});
fs.createReadStream(p).pipe(response);
} else {
var indexPath = path.join(p, 'index');
fs.stat(indexPath, function(err, stats) {
if (err || !stats || !stats.isFile()) {
response.writeHead(404, {
'Cache-Control': 'max-age=7200',
'Content-Type': 'text/plain'
});
return response.end("Not found");
}
response.writeHead(200, {
'Cache-Control': 'max-age=7200',
'Content-Type': type || 'text/html'
});
fs.createReadStream(indexPath).pipe(response);
});
}
});
}).resume();
}).listen(process.env.TCB_PORT || commander.port || 8080);
console.log('Server listening on port ' + (process.env.TCB_PORT || commander.port || 8080));
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/mirrors_opencollective/thecodebarbarian.com.git
git@gitee.com:mirrors_opencollective/thecodebarbarian.com.git
mirrors_opencollective
thecodebarbarian.com
thecodebarbarian.com
master

搜索帮助