-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
49 lines (38 loc) · 1.57 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
const express = require('express');
const url = require('./server/controller/url.controller');
const app = express();
const mongoose = require('mongoose');
const cookieParse = require('cookie-parser')
const path = require('path');
// This is the default address for MongoDB.
// Make sure MongoDB is running!
const mongoEndpoint = process.env.MONGODB_URI || 'mongodb://127.0.0.1/pokemon_app';
// useNewUrlParser is not required, but the old parser is deprecated
mongoose.connect(mongoEndpoint, { useNewUrlParser: true });
// Get the connection string
const db = mongoose.connection;
const session = require('express-session')
//...
// This will manage our sesssion data.
// We can use our secret from our JWT tokens
const MongoStore = require('connect-mongo')(session);
app.use(session({secret: process.env.SUPER_SECRET || "SUPER_SECRET",
store: new MongoStore({
mongooseConnection : db,
})}));
// This will create the connection, and throw an error if it doesn't work
db.on('error', console.error.bind(console, 'Error connecting to MongoDB:'));
app.use(cookieParse());
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
// Note that it is common practice got backend APIs in Node to start with the api prefix
// to distinguish them from frontend routes
app.use('/api/url', url);
// const port = process.env.PORT
app.use(express.static(path.join(__dirname, 'build')));
app.get('/*', (req, res) => {
res.sendFile(path.join(__dirname, 'build', 'index.html'));
});
app.listen(process.env.PORT || 3001, function() {
console.log('Starting server');
});