-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathserver.js
72 lines (60 loc) · 1.98 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
62
63
64
65
66
67
68
69
70
71
72
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const fs = require('fs');
const path = require('path');
const glob = require('glob');
const webpack = require('webpack');
const webpackMiddleware = require('webpack-dev-middleware');
const config = require('./webpack.config.js');
app.set('port', (process.env.PORT || 8080));
app.use(bodyParser.json({limit: '50mb'}));
// Listing different saves
app.get('/listsaves', (req, res) => {
glob('./saves/*.json', (err, files) => {
res.json(files.sort((x, y) => {
return y.match(/[0-9]+/)[0] - x.match(/[0-9]+/);
}));
})
});
if (!process.env.NODE_ENV !== 'production') {
// In dev use webpack dev middleware
const compiler = webpack(config);
const middleware = webpackMiddleware(compiler, {
publicPath: config.output.publicPath,
contentBase: path.join(__dirname, 'dist'),
stats: {
colors: true
}
});
app.use(middleware);
app.get('/', function response(req, res) {
res.write(middleware.fileSystem.readFileSync(path.join(__dirname, 'dist/index.html')));
res.end();
});
} else {
// Run npm start build in prod and serve from static dist
app.use(express.static(path.join(__dirname, '/dist')));
app.get('/', function response(req, res) {
res.sendFile(path.join(__dirname, 'dist/index.html'));
});
}
// Serving the saves files from /saves
app.use('/saves',express.static(path.join(__dirname, '/saves')));
// POST in local on /savestate stores in /saves
app.post('/savestate', (req, res) => {
if (process.env.NODE_ENV != 'production') {
var data = req.body;
if (data) {
fs.writeFile(`./saves/${data.generation}.json`, JSON.stringify(data), e => {
if (e)
console.log(e);
}
);
}
}
res.send({status: true});
});
app.listen(app.get('port'), _ => {
console.log(`App Running on ${app.get('port')}`);
});