-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
47 lines (39 loc) · 1.03 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
import express from 'express';
import cors from 'cors';
import path from 'path';
import bodyParser from 'body-parser';
import {fileURLToPath} from 'url';
const app = express();
const PORT = process.env.PORT || 5000;
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
// Seeded Data
var data =
{
action: "nothing",
}
// Middleware
app.use(cors());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
// Import Built Files
app.use(express.static(path.resolve(__dirname, "./client/build")));
// Endpoints
app.get('/action/', (req, res) => {
res.status(200);
res.send(data.action);
})
app.post('/action/', (req, res) => {
let tempAction = req.body.action
data.action = tempAction
res.status(201);
res.send(data.action);
})
// Use Built Files
app.get("*", function (request, response) {
response.sendFile(path.resolve(__dirname, "./client/build", "index.html"));
});
// Listener
app.listen(PORT, () => {
console.log(`server running on port ${PORT}`);
});