-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
69 lines (54 loc) · 1.46 KB
/
server.js
File metadata and controls
69 lines (54 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
(function () {
"use strict";
var express = require('express');
var exphbs = require('express-handlebars');
var helpers = require('./helpers/all');
var FileSystem = require('./model/filesystem');
var app = express();
var templateEngine = exphbs({
extname: '.hbs',
defaultLayout: 'main',
helpers: helpers
});
app.engine('hbs', templateEngine );
app.set('view engine', 'hbs');
app.use( '/static', express.static(__dirname + '/public'));
app.get('/', function (req, res) {
var directory = fileSystem.root;
fileSystem.exploreDir( directory ).then( function (dirContent) {
res.render('index', {
current: directory,
contents: dirContent
});
});
});
app.get('/file', function (req, res) {
var fRStream = fileSystem.frstream( req.query.id );
fRStream.then( function(stream) {
stream.pipe(res);
});
});
app.get('/directory', function (req, res) {
var directory = req.query.id;
fileSystem.exploreDir( directory ).then( function (dirContent) {
res.render('index', {
current: directory,
contents: dirContent
});
});
});
var args = require('argsparser').parse();
/****
* Arguments:
* + -p: Listening port
* + -d: Directory to watch
*/
var port = args['-p'] || 3000;
var fileSystem = new FileSystem(args['-d'] || '.');
var server = app.listen( port, function () {
var host = server.address().address;
var port = server.address().port;
console.log('Fileserver listining at http://%s:%s...', host, port);
console.log('Watching:', fileSystem.root);
});
}());