mirror of
https://github.com/logsol/chuck.js.git
synced 2026-05-11 10:37:34 +00:00
2025 update!
This commit is contained in:
parent
1df7258b56
commit
e6089687ed
6 changed files with 2487 additions and 358 deletions
|
|
@ -1,105 +1,74 @@
|
|||
define([
|
||||
'http',
|
||||
'node-static',
|
||||
'express',
|
||||
'http',
|
||||
'path',
|
||||
'Server/Api',
|
||||
'fs'
|
||||
],
|
||||
|
||||
function (http, nodeStatic, Api, fs) {
|
||||
function (express, http, path, Api, fs) {
|
||||
|
||||
"use strict";
|
||||
"use strict";
|
||||
|
||||
function HttpServer (options, coordinator) {
|
||||
options.port = options.port || 1234;
|
||||
options.caching = typeof options.caching != 'undefined' ? options.caching : 3600;
|
||||
options.rootDirectory = options.rootDirectory || './';
|
||||
|
||||
this.server = null;
|
||||
this.api = new Api(coordinator);
|
||||
|
||||
this.app = express();
|
||||
this.server = http.createServer(this.app);
|
||||
this.init(options);
|
||||
}
|
||||
|
||||
HttpServer.prototype.init = function (options) {
|
||||
var self = this;
|
||||
var app = this.app;
|
||||
|
||||
//gzip true serves gzip file if there is one with .gz next to the original && if browser supports it
|
||||
var fileServer = new nodeStatic.Server(options.rootDirectory, { cache: options.caching, gzip: true });
|
||||
// Serve static files
|
||||
app.use('/static', express.static(path.join(options.rootDirectory, 'static')));
|
||||
app.use('/app', express.static(path.join(options.rootDirectory, 'app')));
|
||||
|
||||
this.server = http.createServer(
|
||||
function (req, res) {
|
||||
|
||||
var fullBody = '';
|
||||
req.addListener('data', function(chunk) { // doesn't work on Jeenas computer without this
|
||||
fullBody += chunk.toString();
|
||||
});
|
||||
// Serve index.html at root
|
||||
app.get('/', function(req, res) {
|
||||
res.sendFile(path.resolve(options.rootDirectory, 'static/html/index.html'));
|
||||
});
|
||||
|
||||
req.addListener('error', function(err) {
|
||||
console.log('');
|
||||
});
|
||||
|
||||
|
||||
req.addListener('end', function () {
|
||||
|
||||
switch(true) {
|
||||
case req.url == '/':
|
||||
fileServer.serveFile('./static/html/index.html', 200, {}, req, res);
|
||||
console.checkpoint('HTTP Server serves index');
|
||||
break;
|
||||
|
||||
case req.url == '/client.js':
|
||||
fs.exists('./build/client.min.js', function (exists) {
|
||||
if (process.env.NODE_ENV && process.env.NODE_ENV == 'production' && exists) {
|
||||
fileServer.serveFile('./build/client.min.js', 200, {}, req, res);
|
||||
} else {
|
||||
fileServer.serveFile('./client.js', 200, {}, req, res);
|
||||
}
|
||||
});
|
||||
break;
|
||||
|
||||
case req.url == '/client.min.js':
|
||||
fileServer.serveFile('./build/client.min.js', 200, {}, req, res);
|
||||
break;
|
||||
|
||||
case req.url == '/require.js':
|
||||
fileServer.serveFile('./node_modules/requirejs/require.js', 200, {}, req, res);
|
||||
break;
|
||||
|
||||
case req.url == '/screenfull.js':
|
||||
fileServer.serveFile('./node_modules/screenfull/dist/screenfull.js', 200, {}, req, res);
|
||||
break;
|
||||
|
||||
case req.url == '/chart.js':
|
||||
fileServer.serveFile('./node_modules/chart.js/Chart.js', 200, {}, req, res);
|
||||
break;
|
||||
|
||||
case req.url == '/api':
|
||||
self.api.handleCall(fullBody);
|
||||
var status = self.api.isError ? 400 : 200;
|
||||
res.writeHead(status, {"Content-Type": self.api.getContentType()});
|
||||
res.end(self.api.getOutput());
|
||||
self.api.isError = false;
|
||||
break;
|
||||
|
||||
case new RegExp(/^\/app/).test(req.url):
|
||||
fileServer.serve(req, res, function () {
|
||||
self.handleFileError(res)
|
||||
});
|
||||
break;
|
||||
|
||||
case new RegExp(/^\/static/).test(req.url):
|
||||
fileServer.serve(req, res, function () {
|
||||
self.handleFileError(res)
|
||||
});
|
||||
break;
|
||||
|
||||
default:
|
||||
self.handleFileError(res);
|
||||
break;
|
||||
}
|
||||
});
|
||||
// Serve client.js and minified version
|
||||
app.get('/client.js', function(req, res) {
|
||||
if (process.env.NODE_ENV === 'production' && fs.existsSync(path.resolve(options.rootDirectory, 'build/client.min.js'))) {
|
||||
res.sendFile(path.resolve(options.rootDirectory, 'build/client.min.js'));
|
||||
} else {
|
||||
res.sendFile(path.resolve(options.rootDirectory, 'client.js'));
|
||||
}
|
||||
);
|
||||
});
|
||||
app.get('/client.min.js', function(req, res) {
|
||||
res.sendFile(path.resolve(options.rootDirectory, 'build/client.min.js'));
|
||||
});
|
||||
|
||||
// Serve require.js, screenfull.js, chart.js from node_modules
|
||||
app.get('/require.js', function(req, res) {
|
||||
res.sendFile(path.resolve(options.rootDirectory, 'node_modules/requirejs/require.js'));
|
||||
});
|
||||
app.get('/screenfull.js', function(req, res) {
|
||||
res.sendFile(path.resolve(options.rootDirectory, 'static/vendor/screenfull.js'));
|
||||
});
|
||||
app.get('/chart.js', function(req, res) {
|
||||
// Chart.js v4 uses 'dist/chart.umd.js'
|
||||
res.sendFile(path.resolve(options.rootDirectory, 'node_modules/chart.js/dist/chart.umd.js'));
|
||||
});
|
||||
|
||||
// API endpoint
|
||||
app.post('/api', express.text({type: '*/*'}), function(req, res) {
|
||||
self.api.handleCall(req.body);
|
||||
var status = self.api.isError ? 400 : 200;
|
||||
res.status(status).type(self.api.getContentType()).send(self.api.getOutput());
|
||||
self.api.isError = false;
|
||||
});
|
||||
|
||||
// 404 handler
|
||||
app.use(function(req, res) {
|
||||
res.status(404).send('<h1>404 not ... found</h1>');
|
||||
});
|
||||
|
||||
this.server.once('error', function(err) {
|
||||
if(err.code == 'EADDRINUSE') {
|
||||
|
|
@ -110,7 +79,6 @@ function (http, nodeStatic, Api, fs) {
|
|||
});
|
||||
|
||||
this.server.listen(options.port);
|
||||
|
||||
console.checkpoint('start HTTP server');
|
||||
}
|
||||
|
||||
|
|
@ -118,10 +86,5 @@ function (http, nodeStatic, Api, fs) {
|
|||
return this.server;
|
||||
}
|
||||
|
||||
HttpServer.prototype.handleFileError = function (res) {
|
||||
res.writeHead(404, {'Content-Type': 'text/html'});
|
||||
res.end('<h1>404 not ... found</h1>');
|
||||
}
|
||||
|
||||
return HttpServer;
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue