-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
57 lines (45 loc) · 1.69 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
const express = require('express');
const path = require('path');
const bodyParser = require('body-parser');
const logger = require('morgan');
const io = require('./server/socket/');
const router = require('./server/routes/index');
const authRouter = require('./server/routes/authentication');
const database = require('./server/database');
const passport = require('./server/auth');
const port = process.env.PORT || 5000;
const app = express();
// Body parser setup
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
// Passport initialization
app.use(passport.initialize());
app.use(passport.session());
// Log API requests using morgan
app.use(logger('dev'));
// Enable CORS from the client-side
app.use((req, res, next) => {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'PUT, GET, POST, DELETE, OPTIONS');
res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept, Authorization, Access-Control-Allow-Credentials');
res.header('Access-Control-Allow-Credentials', 'true');
next();
});
app.use('/', router);
app.use('/', authRouter);
// Server static files from React app
app.use(express.static(path.join(__dirname, 'client/build')));
// The "catchall" handler: for any request that doesn't
// match one above, send back React's index.html file.
app.get('*', (req, res) => {
res.sendFile(path.join(`${__dirname}/client/build/index.html`));
// res.sendFile(path.resolve(__dirname, '..', 'build', 'index.html'));
});
const server = app.listen(port, (err) => {
if (err) {
console.log(err);
} else {
console.log(`Password generator listening on ${port}`);
}
});
io.listen(server);