2020-04-04 10:36:08 +03:00
|
|
|
const path = require('path');
|
|
|
|
const express = require('express');
|
2021-04-25 23:03:55 +03:00
|
|
|
const reload = require('reload');
|
2020-04-04 10:36:08 +03:00
|
|
|
const app = express();
|
|
|
|
|
|
|
|
const BUILDDIR = process.argv[2] || './build';
|
|
|
|
const PORT = process.argv[3] || 3000;
|
|
|
|
|
|
|
|
//ROUTE - index.htm
|
2021-04-29 00:36:41 +03:00
|
|
|
app.get('/', (req, res) => {
|
2020-04-04 10:36:08 +03:00
|
|
|
res.sendFile(path.join(__dirname, BUILDDIR, 'index.htm'), {}, function (err) {
|
2021-04-29 00:36:41 +03:00
|
|
|
if (err) {
|
2021-01-13 22:21:43 +03:00
|
|
|
console.log('error sending file', err);
|
2020-04-04 10:36:08 +03:00
|
|
|
} else {
|
2021-01-13 22:21:43 +03:00
|
|
|
console.log("Server: Successfully served index.html");
|
2021-04-29 00:36:41 +03:00
|
|
|
|
2021-04-27 12:00:27 +03:00
|
|
|
/*setTimeout(()=>{
|
2020-04-04 10:36:08 +03:00
|
|
|
console.log('closing server');
|
2021-04-25 23:03:55 +03:00
|
|
|
res.app.server.close();
|
2020-04-04 10:36:08 +03:00
|
|
|
process.exit();
|
2021-04-29 00:36:41 +03:00
|
|
|
},1000*10); */
|
2020-04-04 10:36:08 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-04-29 00:33:53 +03:00
|
|
|
// Better to show landing page rather than 404 on editor page reload
|
|
|
|
app.get('/pixel-editor/app', (req, res) => {
|
|
|
|
res.redirect('/');
|
|
|
|
})
|
|
|
|
|
2020-04-04 10:36:08 +03:00
|
|
|
//ROUTE - other files
|
|
|
|
app.use(express.static(path.join(__dirname, BUILDDIR)));
|
|
|
|
|
2021-04-29 00:36:41 +03:00
|
|
|
// "reload" module allows us to trigger webpage reload automatically on file changes, but inside pixel editor it also
|
|
|
|
// makes browser steal focus from any other window in order to ask user about unsaved changes. It might be quite
|
|
|
|
// intrusive so we decided to give option to choose preferred workflow.
|
|
|
|
if (process.env.RELOAD === "yes") {
|
|
|
|
reload(app).then(() => {
|
|
|
|
//start server
|
|
|
|
app.server = app.listen(PORT, () => {
|
|
|
|
console.log(`Web server listening on port ${PORT} (with reload module)`);
|
|
|
|
})
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
app.listen(PORT, () => {
|
|
|
|
console.log(`Web server listening on port ${PORT}`);
|
2021-04-25 23:03:55 +03:00
|
|
|
})
|
2021-04-29 00:36:41 +03:00
|
|
|
}
|