-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
43 lines (32 loc) · 1.08 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
const db = require('./models')
const express = require('express');
const bodyParser = require('body-parser');
const path = require('path');
var cors = require('cors');
const app = express();
const PORT = process.env.PORT || 8080;
const routes = require('./routes/routes.js');
app.use(cors()); // Use this after the variable declaration
app.listen(PORT, () => {
console.log(`Server listening on port ${PORT}.`);
});
// parsing AJAX requests
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
// allow CORS:
app.use(function (req, res, next) {
res.setHeader('Access-Control-Allow-Origin', 'http://localhost:3000');
res.setHeader("Access-Control-Allow-Methods", "GET,HEAD,OPTIONS,POST,PUT");
next();
});
// routes
app.use('/api/', routes);
app.use(express.static(path.join(__dirname, './index.html')));
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, './index.html'));
});
// Anything that doesn't match the above, send back index.html
app.get('*', (req, res) => {
// const root_path =
res.sendFile(path.join(__dirname, './404.html'));
});