This repository was archived by the owner on May 25, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
56 lines (49 loc) · 1.21 KB
/
Copy pathserver.js
File metadata and controls
56 lines (49 loc) · 1.21 KB
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
// Imports
const express = require('express');
// const { Worker, workerData } = require("worker_threads");
const { Worker } = require('worker_threads');
// Basic settings
const app = express();
const port = 3000;
// Static routes
app.use(express.static('static'));
// Middleware
app.use(express.json({ limit: '10Mb' }));
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();
});
// Send HTML files
app.get('/', (req, res) => res.sendFile('index.html'));
// APIs
app.post('/api/createSchedule', (req, res) => {
// Deploy workers
const myWorker = new Worker('./src/worker/scheduler.js', {
workerData: req.body,
});
myWorker.on('message', (result) =>
res.json({
status: 'success',
result,
})
);
myWorker.on('error', (error) => {
console.log(error);
res.status(500).json({
status: 'failed',
result: [],
});
});
});
// Route everything else to the root
app.get('*', (req, res) => {
res.redirect('/');
});
// listen to something
app.listen(port, () =>
console.log(`Example app listening at http://localhost:${port}`)
);