-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
27 changed files
with
1,615 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# HTTP文件浏览 Version2 | ||
|
||
## 改进 | ||
之前做的[HTTP浏览](http://moyuyc.github.io/2016/05/28/node-express-jade%E5%AE%9E%E7%8E%B0HTTP%E6%96%87%E4%BB%B6%E6%B5%8F%E8%A7%88%E5%99%A8/)是使用express2.x版本做的...,因为参考书比较旧了。 | ||
1. `express2.x`中没有`express4.x`中的`res.sendFile()`方法,之前发送文件是使用的`stream.pipe()`方法,导致不支持继续下载,而且用户不能知道下载进度,在线音乐视频播放也不能选择时间跳跃欣赏。`res.sendFile()`方法可以将本地文件以静态资源发送给用户,所有问题迎刃而解。 | ||
2. 旧版本不支持`java/c/cpp/js/css/html`等代码文件和`md/markdown`文件在线查看,所以进行改进。 | ||
3. 利用`Bootstrap responsive utils`和`Bootstrap grid system`进行响应式布局。 | ||
4. 监控`root.txt`文件,改变root后无需重启服务器。 | ||
5. 去除对`q.js`依赖,使用原生`Promise` | ||
|
||
## 说明 | ||
- `root.txt` :共享文件夹路径 | ||
- `app.js` :入口 | ||
|
||
## 使用 | ||
1. npm install | ||
2. npm start | ||
3. http://localhost:3500/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
var express = require('express'); | ||
var path = require('path'); | ||
var favicon = require('serve-favicon'); | ||
var logger = require('morgan'); | ||
var cookieParser = require('cookie-parser'); | ||
var bodyParser = require('body-parser'); | ||
|
||
var routes = require('./routes/index'); | ||
var users = require('./routes/users'); | ||
|
||
var app = express(); | ||
|
||
// view engine setup | ||
app.set('views', path.join(__dirname, 'views')); | ||
app.set('view engine', 'jade'); | ||
|
||
// uncomment after placing your favicon in /public | ||
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico'))); | ||
app.use(logger('dev')); | ||
app.use(bodyParser.json()); | ||
app.use(bodyParser.urlencoded({ extended: false })); | ||
app.use(cookieParser()); | ||
app.use(express.static(path.join(__dirname, 'public'))); | ||
// app.use(express.static(path.join('/'))); | ||
var fs = require('fs'); | ||
|
||
global.root = fs.readFileSync('./root.txt').toString().split(/\s+/)[0]; | ||
fs.watchFile('./root.txt',function () { | ||
global.root = fs.readFileSync('./root.txt').toString().split(/\s+/)[0]; | ||
}); | ||
app.use('/users', users); | ||
app.use('/*', routes.index); | ||
|
||
app.listen(3500); | ||
|
||
// catch 404 and forward to error handler | ||
app.use(function(req, res, next) { | ||
var err = new Error('Not Found'); | ||
err.status = 404; | ||
next(err); | ||
}); | ||
|
||
// error handlers | ||
|
||
// development error handler | ||
// will print stacktrace | ||
if (app.get('env') === 'development') { | ||
app.use(function(err, req, res, next) { | ||
res.status(err.status || 500); | ||
res.render('error', { | ||
message: err.message, | ||
error: err | ||
}); | ||
}); | ||
} | ||
|
||
// production error handler | ||
// no stacktraces leaked to user | ||
app.use(function(err, req, res, next) { | ||
res.status(err.status || 500); | ||
res.render('error', { | ||
message: err.message, | ||
error: {} | ||
}); | ||
}); | ||
|
||
|
||
module.exports = app; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
#!/usr/bin/env node | ||
|
||
/** | ||
* Module dependencies. | ||
*/ | ||
|
||
var app = require('../app'); | ||
var debug = require('debug')('4.x:server'); | ||
var http = require('http'); | ||
|
||
/** | ||
* Get port from environment and store in Express. | ||
*/ | ||
|
||
var port = normalizePort(process.env.PORT || '3000'); | ||
app.set('port', port); | ||
|
||
/** | ||
* Create HTTP server. | ||
*/ | ||
|
||
var server = http.createServer(app); | ||
|
||
/** | ||
* Listen on provided port, on all network interfaces. | ||
*/ | ||
|
||
server.listen(port); | ||
server.on('error', onError); | ||
server.on('listening', onListening); | ||
|
||
/** | ||
* Normalize a port into a number, string, or false. | ||
*/ | ||
|
||
function normalizePort(val) { | ||
var port = parseInt(val, 10); | ||
|
||
if (isNaN(port)) { | ||
// named pipe | ||
return val; | ||
} | ||
|
||
if (port >= 0) { | ||
// port number | ||
return port; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
/** | ||
* Event listener for HTTP server "error" event. | ||
*/ | ||
|
||
function onError(error) { | ||
if (error.syscall !== 'listen') { | ||
throw error; | ||
} | ||
|
||
var bind = typeof port === 'string' | ||
? 'Pipe ' + port | ||
: 'Port ' + port; | ||
|
||
// handle specific listen errors with friendly messages | ||
switch (error.code) { | ||
case 'EACCES': | ||
console.error(bind + ' requires elevated privileges'); | ||
process.exit(1); | ||
break; | ||
case 'EADDRINUSE': | ||
console.error(bind + ' is already in use'); | ||
process.exit(1); | ||
break; | ||
default: | ||
throw error; | ||
} | ||
} | ||
|
||
/** | ||
* Event listener for HTTP server "listening" event. | ||
*/ | ||
|
||
function onListening() { | ||
var addr = server.address(); | ||
var bind = typeof addr === 'string' | ||
? 'pipe ' + addr | ||
: 'port ' + addr.port; | ||
debug('Listening on ' + bind); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
0 info it worked if it ends with ok | ||
1 verbose cli [ '/usr/local/node-v4.4.7-linux-x64/bin/node', | ||
1 verbose cli '/usr/local/node-v4.4.7-linux-x64/bin/npm', | ||
1 verbose cli 'start' ] | ||
2 info using [email protected] | ||
3 info using [email protected] | ||
4 verbose run-script [ 'prestart', 'start', 'poststart' ] | ||
5 info prestart [email protected] | ||
6 info start [email protected] | ||
7 verbose unsafe-perm in lifecycle true | ||
8 info [email protected] Failed to exec start script | ||
9 verbose stack Error: [email protected] start: `node ./bin/www` | ||
9 verbose stack Exit status 1 | ||
9 verbose stack at EventEmitter.<anonymous> (/usr/local/node-v4.4.7-linux-x64/lib/node_modules/npm/lib/utils/lifecycle.js:217:16) | ||
9 verbose stack at emitTwo (events.js:87:13) | ||
9 verbose stack at EventEmitter.emit (events.js:172:7) | ||
9 verbose stack at ChildProcess.<anonymous> (/usr/local/node-v4.4.7-linux-x64/lib/node_modules/npm/lib/utils/spawn.js:24:14) | ||
9 verbose stack at emitTwo (events.js:87:13) | ||
9 verbose stack at ChildProcess.emit (events.js:172:7) | ||
9 verbose stack at maybeClose (internal/child_process.js:827:16) | ||
9 verbose stack at Process.ChildProcess._handle.onexit (internal/child_process.js:211:5) | ||
10 verbose pkgid [email protected] | ||
11 verbose cwd /mnt/www/tarns_app/http_file | ||
12 error Linux 2.6.32-642.3.1.el6.x86_64 | ||
13 error argv "/usr/local/node-v4.4.7-linux-x64/bin/node" "/usr/local/node-v4.4.7-linux-x64/bin/npm" "start" | ||
14 error node v4.4.7 | ||
15 error npm v2.15.8 | ||
16 error code ELIFECYCLE | ||
17 error [email protected] start: `node ./bin/www` | ||
17 error Exit status 1 | ||
18 error Failed at the [email protected] start script 'node ./bin/www'. | ||
18 error This is most likely a problem with the http-file-express4.x package, | ||
18 error not with npm itself. | ||
18 error Tell the author that this fails on your system: | ||
18 error node ./bin/www | ||
18 error You can get information on how to open an issue for this project with: | ||
18 error npm bugs http-file-express4.x | ||
18 error Or if that isn't available, you can get their info via: | ||
18 error | ||
18 error npm owner ls http-file-express4.x | ||
18 error There is likely additional logging output above. | ||
19 verbose exit [ 1, true ] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
{ | ||
"name": "http-file-express4.x", | ||
"version": "0.0.0", | ||
"private": true, | ||
"scripts": { | ||
"start": "node ./bin/www" | ||
}, | ||
"dependencies": { | ||
"archiver": "^1.0.0", | ||
"body-parser": "~1.13.2", | ||
"cookie-parser": "~1.3.5", | ||
"debug": "~2.2.0", | ||
"express": "^4.13.4", | ||
"highlight.js": "^9.4.0", | ||
"jade": "~1.11.0", | ||
"marked": "^0.3.5", | ||
"morgan": "~1.6.1", | ||
"serve-favicon": "~2.3.0" | ||
}, | ||
"description": "之前做的[HTTP浏览](http://moyuyc.github.io/2016/05/28/node-express-jade%E5%AE%9E%E7%8E%B0HTTP%E6%96%87%E4%BB%B6%E6%B5%8F%E8%A7%88%E5%99%A8/)是使用express2.x版本做的...,因为参考书比较旧了。\r 1. `express2.x`中没有`express4.x`中的`res.sendFile()`方法,之前发送文件是使用的`stream.pipe()`方法,导致不支持继续下载,而且用户不能知道下载进度,在线音乐视频播放也不能选择时间跳跃欣赏。`res.sendFile()`方法可以将本地文件以静态资源发送给用户,所有问题迎刃而解。\r 2. 旧版本不支持`java/c/cpp/js/css/html`等代码文件和`md/markdown`文件在线查看,所以进行改进。\r 3. 利用`Bootstrap responsive utils`和`Bootstrap grid system`进行响应式布局。\r 4. 监控`root.txt`文件,改变root后无需重启服务器。\r 5. 去除对`q.js`依赖,使用原生`Promise`", | ||
"main": "app.js", | ||
"devDependencies": {}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/moyuyc/http-file-explorer-express4.x.git" | ||
}, | ||
"keywords": [ | ||
"node", | ||
"express", | ||
"http-explorer" | ||
], | ||
"author": "moyu", | ||
"license": "ISC", | ||
"bugs": { | ||
"url": "https://github.com/moyuyc/http-file-explorer-express4.x/issues" | ||
}, | ||
"homepage": "https://github.com/moyuyc/http-file-explorer-express4.x#readme" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
/** | ||
* Created by Yc on 2016/6/9. | ||
*/ | ||
!function () { | ||
hljs.initHighlightingOnLoad(); | ||
var i =document.getElementById('html-show'); | ||
i.contentWindow.onload = function(){ | ||
var h= 30+i.contentDocument.body.clientHeight; | ||
if(i.clientHeight<h) | ||
i.style.height = h+'px'; | ||
}; | ||
}() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/** | ||
* Created by Yc on 2016/6/9. | ||
*/ | ||
~function(){ | ||
marked.setOptions({ | ||
highlight: function (code) { | ||
return hljs.highlightAuto(code).value; | ||
} | ||
}); | ||
renderer = new marked.Renderer(); | ||
var map = {}; | ||
renderer.heading = function (text, level) { | ||
var escapedText = text.toLowerCase(); | ||
if(!!map[text]) | ||
escapedText+='-'+map[text]++; | ||
else | ||
map[text]=1; | ||
return '<h' + level + '><a name="' + | ||
escapedText + | ||
'" class="anchor" href="#' + | ||
escapedText + | ||
'"><span class="header-link"></span></a>' + | ||
text + '</h' + level + '>'; | ||
}; | ||
}(); |
Large diffs are not rendered by default.
Oops, something went wrong.
Oops, something went wrong.