-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathserver.js
executable file
·74 lines (58 loc) · 1.93 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
72
73
74
#!/usr/bin/env node
const WebsocketServer = require('./lib/websocket-server');
const path = require('path');
const express = require('express');
const browserify = require('browserify-middleware');
const http = require('http');
const cors = require('cors');
const Env = require('./lib/env');
const fs = require('fs');
function Server (folder, port) {
this.folder = path.join(process.cwd(), folder);
this.port = parseInt(port, 10);
}
/**
* Start the express server and the websocket server
*/
Server.prototype.start = function () {
console.log('[server] Serving scenes in \'' + this.folder + '\' on port ' + this.port + '...');
this.webServer = express();
this.webServer.use(cors());
this.webServer.set('view engine', 'ejs');
this.webServer.use('/client.js', browserify(__dirname + '/client/index.js', {
transform: [
['babelify', {presets: ['es2015']}]
],
fullPaths: true
}));
this.webServer.get('/*.html', (req, res) => {
res.render('show');
});
this.webServer.use(express.static(this.folder));
var httpServer = http.createServer(this.webServer);
httpServer.listen(this.port);
this.websocketServer = new WebsocketServer(httpServer, this.folder);
this.websocketServer.listen();
this.websocketServer.folder = this.folder;
if (Env.supportsAutoReload()) {
fs.watch(this.folder, (event, filename) => {
if (event === 'change') {
this.websocketServer.restartReflectorsByFilename(path.resolve(this.folder, filename));
}
});
}
// this.loadAllScenes();
if (Env.isDevelopment()) {
require('dns').lookup(require('os').hostname(), (err, addr, fam) => {
var url = err ? 'localhost:' + this.port : addr + ':' + this.port;
console.log('\n\thttp://' + url + '/\n');
});
}
};
var scenePath = process.argv[2];
if (!scenePath) {
console.log('Usage: scenevr [scenedirectory]');
process.exit(-1);
}
var server = new Server(scenePath, Env.getPort());
server.start();