-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
71 lines (55 loc) · 1.92 KB
/
server.js
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
70
71
var chokidar = require('chokidar');
var express = require('express');
var app = express();
var server = require('http').createServer(app);
var io = require('socket.io')(server);
var path = require('path');
var fs = require('fs');
var argv = require('minimist')(process.argv.slice(2));
// setup webserver port
var port = process.env.PORT || 3000;
server.listen(port);
var imageFolder = argv._[0];
var imageExtensions = ['.jpg', '.jpeg', '.png', '.gif', '.webp', '.svg'];
// serve modern-normalize
app.use('/modern-normalize', express.static(__dirname + '/node_modules/modern-normalize/'));
// serve static files
app.use(express.static(__dirname + '/public'));
// serve image folder
app.use('/images', express.static(imageFolder));
// return index.html when requesting /
app.get('/', function (req, res) {
res.sendfile(__dirname + '/public/index.html');
});
var allImages = [];
// init file watcher
var watcher = chokidar.watch(imageFolder, {
ignored: /[\/\\]\./,
depth: argv.recursive ? undefined : 0,
persistent: true
});
// init websocket
io.on('connection', function (client) {
console.log('new client connected (total: ' + io.engine.clientsCount + ')');
// send all currently available images
client.emit('allImages', allImages);
});
// notify clients when a new image is added
watcher.on('add', function (filePath) {
if (imageExtensions.indexOf(path.extname(filePath).toLowerCase()) === -1) {
return;
}
var relativeFilePath = path.relative(imageFolder, filePath);
console.log('found new file: ' + relativeFilePath);
fs.stat(filePath, function (err, stats) {
var image = {
src: 'images/' + relativeFilePath,
date: stats.mtime,
lastShown: 0
};
io.emit('newImage', image);
allImages.push(image);
});
});
// now it's all up and running...
console.log('App running under http://' + require('os').hostname() + ':' + port + '/');