forked from SocketCluster/socketcluster-website
-
Notifications
You must be signed in to change notification settings - Fork 0
/
worker.js
70 lines (58 loc) · 2.12 KB
/
worker.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
var fs = require('fs');
var path = require('path');
var express = require('express');
var serveStatic = require('serve-static');
module.exports.run = function (worker) {
console.log(' >> Worker PID:', process.pid);
var app = require('express')();
// Get a reference to our raw Node HTTP server
var httpServer = worker.getHTTPServer();
// Get a reference to our WebSocket server
var scServer = worker.getSCServer();
var mainNavFile = __dirname + '/public/app/shared/nav.html';
var mainNavContent = fs.readFileSync(mainNavFile, {encoding: 'utf8'});
var subNavFile = __dirname + '/public/app/shared/nav-docs.html';
var subNavContent = fs.readFileSync(subNavFile, {encoding: 'utf8'});
var navContent = mainNavContent + subNavContent;
var publicDir = path.normalize(__dirname + '/public/');
var templateTagsRegex = /\{{2,3}[^{}]*\}{2,3}/g;
var escapedFragmentRegex = /^\/[?]_escaped_fragment_=(.*)/;
var fragment, page;
// app.use(function (req, res, next) {
// fragment = req.url.match(escapedFragmentRegex);
// if (fragment) {
// page = fragment[1];
// if (page == '/' || page == '') {
// page = '/index';
// }
// var resourcePath = path.normalize(publicDir + 'app/views' + page + '.html');
//
// if (resourcePath.indexOf(publicDir) > -1) {
// fs.readFile(resourcePath, {encoding: 'utf8'}, function (err, data) {
// if (err) {
// data = '';
// }
// var content = (navContent + data).replace(templateTagsRegex, '');
//
// res.writeHead(200, {
// 'Content-Type': 'text/html'
// });
// res.end(content);
// });
// } else {
// res.writeHead(403, {
// 'Content-Type': 'text/html'
// });
// res.end('Access denied');
// }
// } else {
// next();
// }
// });
app.use(require('prerender-node'));
app.use(serveStatic(__dirname + '/public'));
httpServer.on('request', app);
// var randInterval = setInterval(function () {
// scServer.global.publish('rand', Math.round(Math.random() * 100000));
// }, 1000);
};