Files
HIVE/backend/index.js
2018-12-12 11:46:29 +01:00

100 lines
2.4 KiB
JavaScript

const express = require('express');
const https = require('https');
const http = require('http');
const fs = require('fs');
const chokidar = require('chokidar');
const watcher = chokidar.watch('./app');
const privateKey = fs.readFileSync('certificates/private.key').toString();
const certificate = fs.readFileSync('certificates/public.pem').toString();
const production = process.env.NODE_ENV === 'production';
// This line is from the Node.js HTTPS documentation.
const options = {
key: privateKey,
cert: certificate
};
// Create a service (the app object is just a callback).
const app = express();
// File watcher for development reload
if (!production) {
watcher.on('ready', function () {
watcher.on('all', function () {
console.log("Clearing /dist/ module cache from server");
Object.keys(require.cache).forEach(function (id) {
if (/[\/\\]app[\/\\]/.test(id)) delete require.cache[id];
});
});
});
}
app.use(express.static(require('path').join(__dirname, 'src')));
app.use(function (req, res, next) {
res.sendFile(require('path').join(__dirname, 'index.html'));
//require('./index')(req, res, next);
});
//Create http server and redirect to https.
http.createServer(function (req, res) {
res.writeHead(301, {
"Location": "https://" + req.headers.host + req.url
});
res.end();
}).listen(80);
// Create an HTTPS service identical to the HTTP service.
https.createServer(options, app).listen(443);
// routes
app.get('/hey', function (req, res) {
sendToClient("HO!", res, 200, "text/plain");
});
function getHeader(type) {
return {
"Content-Type": type
};
}
function sendToClient(data, res, code, type) {
res.writeHead(code, getHeader(type));
(type === "text/html" || type === "text") ? res.end(data, "utf-8"): res.end(data);
}
const convict = require('convict');
const config = convict({
db: {
name: {
format: String,
default: ''
},
synchro: {
active: {
format: 'Boolean',
default: false
},
remote_url: {
format: 'url',
default: 'http://localhost:8080/'
}
}
},
secret: {
doc: 'Secret used for session cookies and CSRF tokens',
format: '*',
default: '',
sensitive: true
}
});
//config.loadFile("shared/const.json");
const schema = config.getSchemaString();
require('fs').writeFileSync('shared/const.json', config.toString());