refacto(scipts): reorganized & support dev server
This commit is contained in:
parent
1534edba64
commit
7d1db66a26
@ -1,293 +1,277 @@
|
|||||||
var PORT = 8080; //Set port for the app
|
const { getArgs } = require("./utils");
|
||||||
var accessToken = ""; //Can be set here or as start parameter (node server.js --accesstoken=MYTOKEN)
|
const path = require("path");
|
||||||
var disableSmallestScreen = false; //Can be set to true if you dont want to show (node server.js --disablesmallestscreen=true)
|
|
||||||
var webdav = false; //Can be set to true if you want to allow webdav save (node server.js --webdav=true)
|
|
||||||
|
|
||||||
var fs = require("fs-extra");
|
function startBackendServer(port) {
|
||||||
var express = require('express');
|
var accessToken = ""; //Can be set here or as start parameter (node server.js --accesstoken=MYTOKEN)
|
||||||
var formidable = require('formidable'); //form upload processing
|
var disableSmallestScreen = false; //Can be set to true if you dont want to show (node server.js --disablesmallestscreen=true)
|
||||||
|
var webdav = false; //Can be set to true if you want to allow webdav save (node server.js --webdav=true)
|
||||||
const createDOMPurify = require('dompurify'); //Prevent xss
|
|
||||||
const { JSDOM } = require('jsdom');
|
var fs = require("fs-extra");
|
||||||
const window = (new JSDOM('')).window;
|
var express = require('express');
|
||||||
const DOMPurify = createDOMPurify(window);
|
var formidable = require('formidable'); //form upload processing
|
||||||
|
|
||||||
const { createClient } = require("webdav");
|
const createDOMPurify = require('dompurify'); //Prevent xss
|
||||||
|
const { JSDOM } = require('jsdom');
|
||||||
var s_whiteboard = require("./s_whiteboard.js");
|
const window = (new JSDOM('')).window;
|
||||||
|
const DOMPurify = createDOMPurify(window);
|
||||||
var app = express();
|
|
||||||
app.use(express.static(__dirname + '/public'));
|
const { createClient } = require("webdav");
|
||||||
var server = require('http').Server(app);
|
|
||||||
server.listen(PORT);
|
var s_whiteboard = require("./s_whiteboard.js");
|
||||||
var io = require('socket.io')(server);
|
|
||||||
console.log("Webserver & socketserver running on port:" + PORT);
|
var app = express();
|
||||||
|
app.use(express.static(path.join(__dirname, '..', 'public')));
|
||||||
if (process.env.accesstoken) {
|
var server = require('http').Server(app);
|
||||||
accessToken = process.env.accesstoken;
|
server.listen(port);
|
||||||
}
|
var io = require('socket.io')(server, {path: "/ws-api", });
|
||||||
if (process.env.disablesmallestscreen) {
|
console.log("Webserver & socketserver running on port:" + port);
|
||||||
disablesmallestscreen = true;
|
if (process.env.accesstoken) {
|
||||||
}
|
accessToken = process.env.accesstoken;
|
||||||
if (process.env.webdav) {
|
|
||||||
webdav = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
var startArgs = getArgs();
|
|
||||||
if (startArgs["accesstoken"]) {
|
|
||||||
accessToken = startArgs["accesstoken"];
|
|
||||||
}
|
|
||||||
if (startArgs["disablesmallestscreen"]) {
|
|
||||||
disableSmallestScreen = true;
|
|
||||||
}
|
|
||||||
if (startArgs["webdav"]) {
|
|
||||||
webdav = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (accessToken !== "") {
|
|
||||||
console.log("AccessToken set to: " + accessToken);
|
|
||||||
}
|
|
||||||
if (disableSmallestScreen) {
|
|
||||||
console.log("Disabled showing smallest screen resolution!");
|
|
||||||
}
|
|
||||||
if (webdav) {
|
|
||||||
console.log("Webdav save is enabled!");
|
|
||||||
}
|
|
||||||
|
|
||||||
app.get('/loadwhiteboard', function (req, res) {
|
|
||||||
var wid = req["query"]["wid"];
|
|
||||||
var at = req["query"]["at"]; //accesstoken
|
|
||||||
if (accessToken === "" || accessToken == at) {
|
|
||||||
var ret = s_whiteboard.loadStoredData(wid);
|
|
||||||
res.send(ret);
|
|
||||||
res.end();
|
|
||||||
} else {
|
|
||||||
res.status(401); //Unauthorized
|
|
||||||
res.end();
|
|
||||||
}
|
}
|
||||||
});
|
if (process.env.disablesmallestscreen) {
|
||||||
|
disablesmallestscreen = true;
|
||||||
app.post('/upload', function (req, res) { //File upload
|
|
||||||
var form = new formidable.IncomingForm(); //Receive form
|
|
||||||
var formData = {
|
|
||||||
files: {},
|
|
||||||
fields: {}
|
|
||||||
}
|
}
|
||||||
|
if (process.env.webdav) {
|
||||||
form.on('file', function (name, file) {
|
webdav = true;
|
||||||
formData["files"][file.name] = file;
|
}
|
||||||
});
|
|
||||||
|
var startArgs = getArgs();
|
||||||
form.on('field', function (name, value) {
|
if (startArgs["accesstoken"]) {
|
||||||
formData["fields"][name] = value;
|
accessToken = startArgs["accesstoken"];
|
||||||
});
|
}
|
||||||
|
if (startArgs["disablesmallestscreen"]) {
|
||||||
form.on('error', function (err) {
|
disableSmallestScreen = true;
|
||||||
console.log('File uplaod Error!');
|
}
|
||||||
});
|
if (startArgs["webdav"]) {
|
||||||
|
webdav = true;
|
||||||
form.on('end', function () {
|
}
|
||||||
if (accessToken === "" || accessToken == formData["fields"]["at"]) {
|
|
||||||
progressUploadFormData(formData, function (err) {
|
if (accessToken !== "") {
|
||||||
if (err) {
|
console.log("AccessToken set to: " + accessToken);
|
||||||
if (err == "403") {
|
}
|
||||||
res.status(403);
|
if (disableSmallestScreen) {
|
||||||
} else {
|
console.log("Disabled showing smallest screen resolution!");
|
||||||
res.status(500);
|
}
|
||||||
}
|
if (webdav) {
|
||||||
res.end();
|
console.log("Webdav save is enabled!");
|
||||||
} else {
|
}
|
||||||
res.send("done");
|
|
||||||
}
|
app.get('/api/loadwhiteboard', function (req, res) {
|
||||||
});
|
var wid = req["query"]["wid"];
|
||||||
|
var at = req["query"]["at"]; //accesstoken
|
||||||
|
if (accessToken === "" || accessToken == at) {
|
||||||
|
var ret = s_whiteboard.loadStoredData(wid);
|
||||||
|
res.send(ret);
|
||||||
|
res.end();
|
||||||
} else {
|
} else {
|
||||||
res.status(401); //Unauthorized
|
res.status(401); //Unauthorized
|
||||||
res.end();
|
res.end();
|
||||||
}
|
}
|
||||||
//End file upload
|
|
||||||
});
|
});
|
||||||
form.parse(req);
|
|
||||||
});
|
app.post('/api/upload', function (req, res) { //File upload
|
||||||
|
var form = new formidable.IncomingForm(); //Receive form
|
||||||
function progressUploadFormData(formData, callback) {
|
var formData = {
|
||||||
console.log("Progress new Form Data");
|
files: {},
|
||||||
var fields = escapeAllContentStrings(formData.fields);
|
fields: {}
|
||||||
var files = formData.files;
|
|
||||||
var whiteboardId = fields["whiteboardId"];
|
|
||||||
|
|
||||||
var name = fields["name"] || "";
|
|
||||||
var date = fields["date"] || (+new Date());
|
|
||||||
var filename = whiteboardId + "_" + date + ".png";
|
|
||||||
var webdavaccess = fields["webdavaccess"] || false;
|
|
||||||
try {
|
|
||||||
webdavaccess = JSON.parse(webdavaccess);
|
|
||||||
} catch (e) {
|
|
||||||
webdavaccess = false;
|
|
||||||
}
|
|
||||||
fs.ensureDir("./public/uploads", function (err) {
|
|
||||||
if (err) {
|
|
||||||
console.log("Could not create upload folder!", err);
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
var imagedata = fields["imagedata"];
|
|
||||||
if (imagedata && imagedata != "") { //Save from base64 data
|
form.on('file', function (name, file) {
|
||||||
imagedata = imagedata.replace(/^data:image\/png;base64,/, "").replace(/^data:image\/jpeg;base64,/, "");
|
formData["files"][file.name] = file;
|
||||||
console.log(filename, "uploaded");
|
});
|
||||||
fs.writeFile('./public/uploads/' + filename, imagedata, 'base64', function (err) {
|
|
||||||
if (err) {
|
form.on('field', function (name, value) {
|
||||||
console.log("error", err);
|
formData["fields"][name] = value;
|
||||||
callback(err);
|
});
|
||||||
} else {
|
|
||||||
if (webdavaccess) { //Save image to webdav
|
form.on('error', function (err) {
|
||||||
if (webdav) {
|
console.log('File uplaod Error!');
|
||||||
saveImageToWebdav('./public/uploads/' + filename, filename, webdavaccess, function (err) {
|
});
|
||||||
if (err) {
|
|
||||||
console.log("error", err);
|
form.on('end', function () {
|
||||||
callback(err);
|
if (accessToken === "" || accessToken == formData["fields"]["at"]) {
|
||||||
} else {
|
progressUploadFormData(formData, function (err) {
|
||||||
callback();
|
if (err) {
|
||||||
}
|
if (err == "403") {
|
||||||
})
|
res.status(403);
|
||||||
} else {
|
} else {
|
||||||
callback("Webdav is not enabled on the server!");
|
res.status(500);
|
||||||
}
|
}
|
||||||
|
res.end();
|
||||||
} else {
|
} else {
|
||||||
callback();
|
res.send("done");
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
res.status(401); //Unauthorized
|
||||||
|
res.end();
|
||||||
|
}
|
||||||
|
//End file upload
|
||||||
|
});
|
||||||
|
form.parse(req);
|
||||||
|
});
|
||||||
|
|
||||||
|
function progressUploadFormData(formData, callback) {
|
||||||
|
console.log("Progress new Form Data");
|
||||||
|
var fields = escapeAllContentStrings(formData.fields);
|
||||||
|
var files = formData.files;
|
||||||
|
var whiteboardId = fields["whiteboardId"];
|
||||||
|
|
||||||
|
var name = fields["name"] || "";
|
||||||
|
var date = fields["date"] || (+new Date());
|
||||||
|
var filename = whiteboardId + "_" + date + ".png";
|
||||||
|
var webdavaccess = fields["webdavaccess"] || false;
|
||||||
|
try {
|
||||||
|
webdavaccess = JSON.parse(webdavaccess);
|
||||||
|
} catch (e) {
|
||||||
|
webdavaccess = false;
|
||||||
|
}
|
||||||
|
fs.ensureDir("./public/uploads", function (err) {
|
||||||
|
if (err) {
|
||||||
|
console.log("Could not create upload folder!", err);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
var imagedata = fields["imagedata"];
|
||||||
|
if (imagedata && imagedata != "") { //Save from base64 data
|
||||||
|
imagedata = imagedata.replace(/^data:image\/png;base64,/, "").replace(/^data:image\/jpeg;base64,/, "");
|
||||||
|
console.log(filename, "uploaded");
|
||||||
|
fs.writeFile('./public/uploads/' + filename, imagedata, 'base64', function (err) {
|
||||||
|
if (err) {
|
||||||
|
console.log("error", err);
|
||||||
|
callback(err);
|
||||||
|
} else {
|
||||||
|
if (webdavaccess) { //Save image to webdav
|
||||||
|
if (webdav) {
|
||||||
|
saveImageToWebdav('./public/uploads/' + filename, filename, webdavaccess, function (err) {
|
||||||
|
if (err) {
|
||||||
|
console.log("error", err);
|
||||||
|
callback(err);
|
||||||
|
} else {
|
||||||
|
callback();
|
||||||
|
}
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
callback("Webdav is not enabled on the server!");
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
callback();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
callback("no imagedata!");
|
||||||
|
console.log("No image Data found for this upload!", name);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function saveImageToWebdav(imagepath, filename, webdavaccess, callback) {
|
||||||
|
if (webdavaccess) {
|
||||||
|
var webdavserver = webdavaccess["webdavserver"] || "";
|
||||||
|
var webdavpath = webdavaccess["webdavpath"] || "/";
|
||||||
|
var webdavusername = webdavaccess["webdavusername"] || "";
|
||||||
|
var webdavpassword = webdavaccess["webdavpassword"] || "";
|
||||||
|
|
||||||
|
const client = createClient(
|
||||||
|
webdavserver,
|
||||||
|
{
|
||||||
|
username: webdavusername,
|
||||||
|
password: webdavpassword
|
||||||
}
|
}
|
||||||
|
)
|
||||||
|
client.getDirectoryContents(webdavpath).then((items) => {
|
||||||
|
var cloudpath = webdavpath+ '' + filename;
|
||||||
|
console.log("webdav saving to:", cloudpath);
|
||||||
|
fs.createReadStream(imagepath).pipe(client.createWriteStream(cloudpath));
|
||||||
|
callback();
|
||||||
|
}).catch((error) => {
|
||||||
|
callback("403");
|
||||||
|
console.log("Could not connect to webdav!")
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
callback("no imagedata!");
|
callback("Error: no access data!")
|
||||||
console.log("No image Data found for this upload!", name);
|
|
||||||
}
|
}
|
||||||
});
|
}
|
||||||
}
|
|
||||||
|
var smallestScreenResolutions = {};
|
||||||
function saveImageToWebdav(imagepath, filename, webdavaccess, callback) {
|
io.on('connection', function (socket) {
|
||||||
if (webdavaccess) {
|
var whiteboardId = null;
|
||||||
var webdavserver = webdavaccess["webdavserver"] || "";
|
|
||||||
var webdavpath = webdavaccess["webdavpath"] || "/";
|
socket.on('disconnect', function () {
|
||||||
var webdavusername = webdavaccess["webdavusername"] || "";
|
if (smallestScreenResolutions && smallestScreenResolutions[whiteboardId] && socket && socket.id) {
|
||||||
var webdavpassword = webdavaccess["webdavpassword"] || "";
|
delete smallestScreenResolutions[whiteboardId][socket.id];
|
||||||
|
|
||||||
const client = createClient(
|
|
||||||
webdavserver,
|
|
||||||
{
|
|
||||||
username: webdavusername,
|
|
||||||
password: webdavpassword
|
|
||||||
}
|
}
|
||||||
)
|
socket.broadcast.emit('refreshUserBadges', null); //Removes old user Badges
|
||||||
client.getDirectoryContents(webdavpath).then((items) => {
|
sendSmallestScreenResolution();
|
||||||
var cloudpath = webdavpath+ '' + filename;
|
|
||||||
console.log("webdav saving to:", cloudpath);
|
|
||||||
fs.createReadStream(imagepath).pipe(client.createWriteStream(cloudpath));
|
|
||||||
callback();
|
|
||||||
}).catch((error) => {
|
|
||||||
callback("403");
|
|
||||||
console.log("Could not connect to webdav!")
|
|
||||||
});
|
});
|
||||||
} else {
|
|
||||||
callback("Error: no access data!")
|
socket.on('drawToWhiteboard', function (content) {
|
||||||
}
|
content = escapeAllContentStrings(content);
|
||||||
}
|
if (accessToken === "" || accessToken == content["at"]) {
|
||||||
|
socket.broadcast.to(whiteboardId).emit('drawToWhiteboard', content); //Send to all users in the room (not own socket)
|
||||||
var smallestScreenResolutions = {};
|
s_whiteboard.handleEventsAndData(content); //save whiteboardchanges on the server
|
||||||
io.on('connection', function (socket) {
|
} else {
|
||||||
var whiteboardId = null;
|
socket.emit('wrongAccessToken', true);
|
||||||
|
|
||||||
socket.on('disconnect', function () {
|
|
||||||
if (smallestScreenResolutions && smallestScreenResolutions[whiteboardId] && socket && socket.id) {
|
|
||||||
delete smallestScreenResolutions[whiteboardId][socket.id];
|
|
||||||
}
|
|
||||||
socket.broadcast.emit('refreshUserBadges', null); //Removes old user Badges
|
|
||||||
sendSmallestScreenResolution();
|
|
||||||
});
|
|
||||||
|
|
||||||
socket.on('drawToWhiteboard', function (content) {
|
|
||||||
content = escapeAllContentStrings(content);
|
|
||||||
if (accessToken === "" || accessToken == content["at"]) {
|
|
||||||
socket.broadcast.to(whiteboardId).emit('drawToWhiteboard', content); //Send to all users in the room (not own socket)
|
|
||||||
s_whiteboard.handleEventsAndData(content); //save whiteboardchanges on the server
|
|
||||||
} else {
|
|
||||||
socket.emit('wrongAccessToken', true);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
socket.on('joinWhiteboard', function (content) {
|
|
||||||
content = escapeAllContentStrings(content);
|
|
||||||
if (accessToken === "" || accessToken == content["at"]) {
|
|
||||||
whiteboardId = content["wid"];
|
|
||||||
socket.join(whiteboardId); //Joins room name=wid
|
|
||||||
smallestScreenResolutions[whiteboardId] = smallestScreenResolutions[whiteboardId] ? smallestScreenResolutions[whiteboardId] : {};
|
|
||||||
smallestScreenResolutions[whiteboardId][socket.id] = content["windowWidthHeight"] || { w: 10000, h: 10000 };
|
|
||||||
sendSmallestScreenResolution();
|
|
||||||
} else {
|
|
||||||
socket.emit('wrongAccessToken', true);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
socket.on('updateScreenResolution', function (content) {
|
|
||||||
content = escapeAllContentStrings(content);
|
|
||||||
if (accessToken === "" || accessToken == content["at"]) {
|
|
||||||
smallestScreenResolutions[whiteboardId][socket.id] = content["windowWidthHeight"] || { w: 10000, h: 10000 };
|
|
||||||
sendSmallestScreenResolution();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
function sendSmallestScreenResolution() {
|
|
||||||
if (disableSmallestScreen) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
var smallestWidth = 10000;
|
|
||||||
var smallestHeight = 10000;
|
|
||||||
for (var i in smallestScreenResolutions[whiteboardId]) {
|
|
||||||
smallestWidth = smallestWidth > smallestScreenResolutions[whiteboardId][i]["w"] ? smallestScreenResolutions[whiteboardId][i]["w"] : smallestWidth;
|
|
||||||
smallestHeight = smallestHeight > smallestScreenResolutions[whiteboardId][i]["h"] ? smallestScreenResolutions[whiteboardId][i]["h"] : smallestHeight;
|
|
||||||
}
|
|
||||||
io.to(whiteboardId).emit('updateSmallestScreenResolution', { w: smallestWidth, h: smallestHeight });
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
//Prevent cross site scripting (xss)
|
|
||||||
function escapeAllContentStrings(content, cnt) {
|
|
||||||
if (!cnt)
|
|
||||||
cnt = 0;
|
|
||||||
|
|
||||||
if (typeof (content) === "string") {
|
|
||||||
return DOMPurify.sanitize(content);
|
|
||||||
}
|
|
||||||
for (var i in content) {
|
|
||||||
if (typeof (content[i]) === "string") {
|
|
||||||
content[i] = DOMPurify.sanitize(content[i]);
|
|
||||||
} if (typeof (content[i]) === "object" && cnt < 10) {
|
|
||||||
content[i] = escapeAllContentStrings(content[i], ++cnt);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return content;
|
|
||||||
}
|
|
||||||
|
|
||||||
function getArgs() {
|
|
||||||
const args = {}
|
|
||||||
process.argv
|
|
||||||
.slice(2, process.argv.length)
|
|
||||||
.forEach(arg => {
|
|
||||||
// long arg
|
|
||||||
if (arg.slice(0, 2) === '--') {
|
|
||||||
const longArg = arg.split('=')
|
|
||||||
args[longArg[0].slice(2, longArg[0].length)] = longArg[1]
|
|
||||||
}
|
}
|
||||||
// flags
|
});
|
||||||
else if (arg[0] === '-') {
|
|
||||||
const flags = arg.slice(1, arg.length).split('')
|
socket.on('joinWhiteboard', function (content) {
|
||||||
flags.forEach(flag => {
|
content = escapeAllContentStrings(content);
|
||||||
args[flag] = true
|
if (accessToken === "" || accessToken == content["at"]) {
|
||||||
})
|
whiteboardId = content["wid"];
|
||||||
|
socket.join(whiteboardId); //Joins room name=wid
|
||||||
|
smallestScreenResolutions[whiteboardId] = smallestScreenResolutions[whiteboardId] ? smallestScreenResolutions[whiteboardId] : {};
|
||||||
|
smallestScreenResolutions[whiteboardId][socket.id] = content["windowWidthHeight"] || { w: 10000, h: 10000 };
|
||||||
|
sendSmallestScreenResolution();
|
||||||
|
} else {
|
||||||
|
socket.emit('wrongAccessToken', true);
|
||||||
}
|
}
|
||||||
})
|
});
|
||||||
return args
|
|
||||||
|
socket.on('updateScreenResolution', function (content) {
|
||||||
|
content = escapeAllContentStrings(content);
|
||||||
|
if (accessToken === "" || accessToken == content["at"]) {
|
||||||
|
smallestScreenResolutions[whiteboardId][socket.id] = content["windowWidthHeight"] || { w: 10000, h: 10000 };
|
||||||
|
sendSmallestScreenResolution();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
function sendSmallestScreenResolution() {
|
||||||
|
if (disableSmallestScreen) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
var smallestWidth = 10000;
|
||||||
|
var smallestHeight = 10000;
|
||||||
|
for (var i in smallestScreenResolutions[whiteboardId]) {
|
||||||
|
smallestWidth = smallestWidth > smallestScreenResolutions[whiteboardId][i]["w"] ? smallestScreenResolutions[whiteboardId][i]["w"] : smallestWidth;
|
||||||
|
smallestHeight = smallestHeight > smallestScreenResolutions[whiteboardId][i]["h"] ? smallestScreenResolutions[whiteboardId][i]["h"] : smallestHeight;
|
||||||
|
}
|
||||||
|
io.to(whiteboardId).emit('updateSmallestScreenResolution', { w: smallestWidth, h: smallestHeight });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
//Prevent cross site scripting (xss)
|
||||||
|
function escapeAllContentStrings(content, cnt) {
|
||||||
|
if (!cnt)
|
||||||
|
cnt = 0;
|
||||||
|
|
||||||
|
if (typeof (content) === "string") {
|
||||||
|
return DOMPurify.sanitize(content);
|
||||||
|
}
|
||||||
|
for (var i in content) {
|
||||||
|
if (typeof (content[i]) === "string") {
|
||||||
|
content[i] = DOMPurify.sanitize(content[i]);
|
||||||
|
} if (typeof (content[i]) === "object" && cnt < 10) {
|
||||||
|
content[i] = escapeAllContentStrings(content[i], ++cnt);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return content;
|
||||||
|
}
|
||||||
|
|
||||||
|
process.on('unhandledRejection', error => {
|
||||||
|
// Will print "unhandledRejection err is not defined"
|
||||||
|
console.log('unhandledRejection', error.message);
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
process.on('unhandledRejection', error => {
|
module.exports = startBackendServer;
|
||||||
// Will print "unhandledRejection err is not defined"
|
|
||||||
console.log('unhandledRejection', error.message);
|
|
||||||
})
|
|
32
scripts/server-frontend-dev.js
Normal file
32
scripts/server-frontend-dev.js
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
const webpack = require("webpack");
|
||||||
|
const WebpackDevServer = require("webpack-dev-server");
|
||||||
|
const config = require("../config/webpack.dev");
|
||||||
|
|
||||||
|
const devServerConfig = {
|
||||||
|
hot: true,
|
||||||
|
inline: true,
|
||||||
|
stats: {
|
||||||
|
children: false,
|
||||||
|
maxModules: 0
|
||||||
|
},
|
||||||
|
proxy: {
|
||||||
|
'/api': 'http://localhost:3000',
|
||||||
|
'/ws-api': {
|
||||||
|
target: 'ws://localhost:3000',
|
||||||
|
ws: true,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function startFrontendDevServer(port) {
|
||||||
|
new WebpackDevServer(webpack(config), devServerConfig)
|
||||||
|
.listen(port, (err) => {
|
||||||
|
if (err) {
|
||||||
|
console.log(err);
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log("Listening on port " + port);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = startFrontendDevServer;
|
25
scripts/server.js
Normal file
25
scripts/server.js
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
const { getArgs } = require("./utils");
|
||||||
|
const startFrontendDevServer = require("./server-frontend-dev");
|
||||||
|
const startBackendServer = require("./server-backend");
|
||||||
|
|
||||||
|
const SERVER_MODES = {
|
||||||
|
PRODUCTION: 1,
|
||||||
|
DEVELOPMENT: 2
|
||||||
|
}
|
||||||
|
|
||||||
|
const args = getArgs();
|
||||||
|
|
||||||
|
if ( typeof args.mode === "undefined" || (args.mode !== "production" && args.mode !== "development")) {
|
||||||
|
throw new Error("--mode=development or --mode=production is expected")
|
||||||
|
}
|
||||||
|
|
||||||
|
const server_mode = args.mode === "production" ? SERVER_MODES.PRODUCTION : SERVER_MODES.DEVELOPMENT;
|
||||||
|
|
||||||
|
if (server_mode === SERVER_MODES.DEVELOPMENT){
|
||||||
|
console.info("Starting server in development mode.");
|
||||||
|
startFrontendDevServer(8080);
|
||||||
|
startBackendServer(3000);
|
||||||
|
} else {
|
||||||
|
console.info("Starting server in production mode.");
|
||||||
|
startBackendServer(8080);
|
||||||
|
}
|
22
scripts/utils.js
Normal file
22
scripts/utils.js
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
function getArgs() {
|
||||||
|
const args = {}
|
||||||
|
process.argv
|
||||||
|
.slice(2, process.argv.length)
|
||||||
|
.forEach(arg => {
|
||||||
|
// long arg
|
||||||
|
if (arg.slice(0, 2) === '--') {
|
||||||
|
const longArg = arg.split('=')
|
||||||
|
args[longArg[0].slice(2, longArg[0].length)] = longArg[1]
|
||||||
|
}
|
||||||
|
// flags
|
||||||
|
else if (arg[0] === '-') {
|
||||||
|
const flags = arg.slice(1, arg.length).split('')
|
||||||
|
flags.forEach(flag => {
|
||||||
|
args[flag] = true
|
||||||
|
})
|
||||||
|
}
|
||||||
|
})
|
||||||
|
return args
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports.getArgs = getArgs;
|
Loading…
Reference in New Issue
Block a user