node-tool-utils
Advanced tools
Comparing version 1.3.0 to 1.4.0
@@ -0,1 +1,10 @@ | ||
# [1.4.0](https://github.com/hubcarl/node-tool-utils/compare/1.3.0...1.4.0) (2019-10-21) | ||
### Features | ||
* set header[Access-Control-Allow-Origin]=* for CORS and auto serv no file ext ([60fc0c7](https://github.com/hubcarl/node-tool-utils/commit/60fc0c7)) | ||
# [1.3.0](https://github.com/hubcarl/node-tool-utils/compare/1.2.3...1.3.0) (2019-09-07) | ||
@@ -2,0 +11,0 @@ |
@@ -75,11 +75,25 @@ 'use strict'; | ||
httpserver.beforeServe = (request, response, body, encoding) => { | ||
['md', 'lock', 'log', 'ts', 'jsx', 'tsx', 'tpl', 'sql'].forEach(ext => { | ||
response.setHeader('Access-Control-Allow-Origin', '*'); | ||
// image | ||
httpserver.config.contentType['webp'] = 'image/webp'; | ||
httpserver.config.contentType['svg'] = 'image/svg+xml'; | ||
// special | ||
httpserver.config.contentType['xml'] = 'application/xml'; | ||
// text | ||
const textTypes = ['md', 'lock', 'log', 'ts', 'jsx', 'tsx', 'tpl', 'sql', 'less', 'styl', 'stylus', 'scss']; | ||
textTypes.forEach(ext => { | ||
httpserver.config.contentType[ext] = 'text/plain'; | ||
}); | ||
'mp4|webm|ogg|mp3|wav|flac|aac|zip|tar|tar.gz|tar.xz|doc|pdf|xls|msi|7z|pkg|ipa|woff|woff2|eot|ttf|otf'.split('|').forEach(v => { | ||
// stream | ||
const streamTypes = 'mp4|webm|ogg|mp3|wav|flac|aac|zip|tar|tar.gz|tar.xz|doc|docx|xls|ppt|pdf|msi|7z|pkg|ipa|woff|woff2|eot|ttf|otf'.split('|'); | ||
streamTypes.forEach(v => { | ||
httpserver.config.contentType[v] = 'application/octet-stream'; | ||
}); | ||
const pathname = request.uri.pathname; | ||
const filepath = path.join(root, pathname.replace(/^\//, '')); | ||
if (response.statusCode === 415) { | ||
const pathname = request.uri.pathname; | ||
const filepath = path.join(root, pathname.replace(/^\//, '')); | ||
if (fs.existsSync(filepath)) { | ||
@@ -98,2 +112,17 @@ if (fs.statSync(filepath).isDirectory()) { | ||
} | ||
} else if (response.statusCode === 404 ) { | ||
// auto match file ext | ||
if (filepath.indexOf('.') === -1) { | ||
const extKeys = Object.keys(httpserver.config.contentType); | ||
const ext = extKeys.find(ext => { | ||
const file = `${filepath}.${ext}`; | ||
return fs.existsSync(file); | ||
}); | ||
if (ext) { | ||
response.setHeader('Content-Type', httpserver.config.contentType[ext]); | ||
response.statusCode = 200; | ||
body.value = fs.readFileSync(`${filepath}.${ext}`); | ||
return; | ||
} | ||
} | ||
} | ||
@@ -129,3 +158,3 @@ }; | ||
const pathname = file.replace(root, ''); | ||
return { | ||
return { | ||
pathname, | ||
@@ -136,3 +165,3 @@ filepath: file, | ||
}); | ||
} | ||
} | ||
return [{ filename, pathname, filepath: rootFilePath }]; | ||
@@ -139,0 +168,0 @@ } |
{ | ||
"name": "node-tool-utils", | ||
"version": "1.3.0", | ||
"version": "1.4.0", | ||
"description": "node cross-platform tool library", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
16724
325