forked from vuejsdevelopers/vuejs-calendar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
42 lines (33 loc) · 1.21 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
require('dotenv').config({ silent: true });
const express = require('express');
const app = express();
const path = require('path');
const fs = require('fs');
const http = require('http');
const moment = require('moment-timezone');
moment.tz.setDefault('UTC');
const serialize = require('serialize-javascript');
app.use('/public', express.static(path.join(__dirname, 'public')));
let events = [];
app.get('/', (req, res) => {
let template = fs.readFileSync(path.resolve('./index.html'), 'utf-8');
let contentMarker = '<!--APP-->';
res.send(template.replace(contentMarker, `<script>var __INITIAL_STATE__ = ${serialize(events)}</script>`));
});
app.use(require('body-parser').json());
app.post('/events',(req, res) => {
events.push(req.body);
res.sendStatus(200);
});
const server = http.createServer(app);
if (process.env.NODE_ENV === 'development') {
const reload = require('reload');
const reloadServer = reload(server, app);
require('./webpack-dev-middleware').init(app);
}
server.listen(process.env.PORT, function () {
console.log(`Example app listening on port ${process.env.PORT}!`);
if (process.env.NODE_ENV === 'development') {
require("open")(`http://localhost:${process.env.PORT}`);
}
});