-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.js
61 lines (56 loc) · 1.82 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/**
* Loads the environment variables and starts the server.
*/
require("dotenv").config()
const express = require("express")
const tasks = require("./tasks.js")
const run = require("./run.js")
const nodes = require("./nodes.js")
const stores = require("./stores.js")
const fs = require("fs")
let node = require("hyper-ipc-secure")()
async function exitHandler(options, exitCode) {
console.log('restarted node')
node = require("hyper-ipc-secure")()
}
process.on('exit', exitHandler.bind(null, 'cleanup'))
process.on('uncaughtException', exitHandler.bind(null, 'uncaughtException'))
process.on('unhandledRejection', exitHandler.bind(null, 'unhandledRejection'))
/**
* Creates a folder if it doesn't exist.
* @param {string} folderPath - The path of the folder.
*/
function createFolderIfNotExists(folderPath) {
if (!fs.existsSync(folderPath)) {
// If the folder doesn't exist, create it
fs.mkdirSync(folderPath)
console.log(`Folder "${folderPath}" created.`)
} else {
console.log(`Folder "${folderPath}" already exists.`)
}
}
createFolderIfNotExists("saves")
const app = express()
const port = process.env.PORT || 3011
app.use(express.json())
app.use(express.urlencoded({
extended: true
}))
app.use((function (req, res, next) {
res.setHeader("Access-Control-Allow-Origin", "*")
res.setHeader("Access-Control-Allow-Methods", "GET, POST, OPTIONS, PUT, PATCH, DELETE")
res.setHeader("Access-Control-Allow-Headers", "X-Requested-With, content-type")
res.setHeader("Access-Control-Allow-Credentials", true)
next()
}))
const vault = nodes(node)
console.log(vault)
app.use(express.static('public'))
app.use("/vault", vault)
app.use("/task", tasks(node))
app.use("/store", stores())
app.use("/run", run(node))
app.use(express.static('public'))
app.listen(port, (() => {
console.log(`Example app listening on port ${port}`)
}))