From ea1ff833c485b8cf3bd07337f8bab546990505fd Mon Sep 17 00:00:00 2001 From: tom Date: Sun, 24 May 2020 18:52:59 +0200 Subject: [PATCH] update start in background --- package.json | 3 ++- server.js | 12 ++++++++++-- server/express.js | 37 +++++++++++++++++++++++++++++++++++++ {src => server}/web.js | 11 ++++------- start.bat | 3 ++- start.sh | 3 ++- 6 files changed, 57 insertions(+), 12 deletions(-) create mode 100644 server/express.js rename {src => server}/web.js (88%) diff --git a/package.json b/package.json index c92f077..c18fd58 100644 --- a/package.json +++ b/package.json @@ -69,7 +69,8 @@ "os-homedir": "^1.0.2", "papaparse": "^4.6.0", "pug": "^2.0.4", - "shelljs": "^0.8.2" + "shelljs": "^0.8.2", + "forever": "^3.0.0" }, "build": { "appId": "com.electron.promagen-one", diff --git a/server.js b/server.js index a6d44fd..a9b6ab9 100644 --- a/server.js +++ b/server.js @@ -1,3 +1,11 @@ // Start Express web app based on Express -const web = require('./src/web'); -return new web(); + +// CONFIG +const PublicConfig = require('../config/projects'); +var localConfig = new PublicConfig(); + +const web = require('./server/web'); +return new web('PROMAGEN ONE ', 'localhost', localConfig.port, '../public'); + +// const web = require('./server/express'); +// return new web('PROMAGEN ONE ', 'localhost', localConfig.port, '../public'); diff --git a/server/express.js b/server/express.js new file mode 100644 index 0000000..fac89a2 --- /dev/null +++ b/server/express.js @@ -0,0 +1,37 @@ +module.exports = function (application, domain = 'localhost', port = 3000, public_src = "./") { + + const express = require('express'); + + const app = express(); + port = process.env.PORT || port; + + // NEW - Add CORS headers - see https://enable-cors.org/server_expressjs.html + app.use(function (req, res, next) { + res.header("Access-Control-Allow-Origin", "*"); + res.header( + "Access-Control-Allow-Headers", + "Origin, X-Requested-With, Content-Type, Accept" + ); + next(); + }); + + app.use(express.static(public_src)); + + // API endpoint + app.get("/api/ping", (req, res) => { + res.send({ + msg: "Hello, World" + }); + }); + + + // app.get('/', (req, res) => { + // res.send('An alligator approaches!'); + // }); + + // app.listen(PORT, () => console.log(`listening on ${PORT}`)); + var url = 'http://' + domain + ':' + port; + app.listen(port, () => console.log(application + ' is listening on: ' + url)); + + return app; +}; diff --git a/src/web.js b/server/web.js similarity index 88% rename from src/web.js rename to server/web.js index 9939503..0989e2e 100644 --- a/src/web.js +++ b/server/web.js @@ -1,4 +1,4 @@ -module.exports = function () { +module.exports = function (application, domain = 'localhost', port = 3000, public_src = "./") { const createError = require('http-errors'); const express = require('express'); @@ -7,10 +7,6 @@ module.exports = function () { const logger = require('morgan'); //const auth = require('./auth'); -// CONFIG - const PublicConfig = require('../config/projects'); - var localConfig = new PublicConfig(); - //var FileList = []; const indexRouter = require('../routes'); const batRouter = require('../routes/bat'); @@ -33,7 +29,8 @@ module.exports = function () { web.use(express.json()); web.use(express.urlencoded({extended: false})); web.use(cookieParser()); - var public_path = path.join(__dirname, '../public'); + + var public_path = path.join(__dirname, public_src); console.log('public_path', public_path); web.use(express.static(public_path)); @@ -60,7 +57,7 @@ module.exports = function () { res.render('error', {title: title}); }); - web.listen(localConfig.port); + web.listen(port); return web; }; diff --git a/start.bat b/start.bat index 90070d1..658ff22 100644 --- a/start.bat +++ b/start.bat @@ -4,6 +4,7 @@ WHERE node IF %ERRORLEVEL% NEQ 0 ( echo nodejs is not installed on this system! ) else ( - node server.js + ::node server.js + forever start server.js ) diff --git a/start.sh b/start.sh index 8092b24..ed31cba 100644 --- a/start.sh +++ b/start.sh @@ -3,4 +3,5 @@ #dir_path=$(dirname $full_path) #echo $dir_path #node $dir_path/server.js -node server.js +#node server.js +forever start server.js