-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
76 lines (61 loc) · 1.7 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
73
74
75
76
const dotenv = require("dotenv");
dotenv.config();
// Environment Variables
const PORT = process.env.PORT;
// Libraries
const express = require("express");
const app = express();
const bodyParser = require('body-parser');
// MongoDB and Sessions
const mongoose = require('mongoose');
const cookieParser = require('cookie-parser');
const session = require('express-session');
const MongoStore = require('connect-mongo');
// Allow cross-origin-requests
const cors = require("cors");
app.use(
cors({
origin: "*",
})
)
// Connects to the MongoDB
mongoose.connect(process.env.MONGO_DB_URL)
.then(() =>{ console.log("Connected to DB") })
.catch(err => { console.log(`Error: ${err.message}`)});
const db = mongoose.connection;
//Creates a store object with options
const store = new MongoStore({
mongoUrl: process.env.MONGO_DB_URL,
dbName: "Stanify",
collectionName: 'sessions',
autoRemove: 'native',
ttl: 24 * 7 * 60 * 60
})
// Uses a cookie parser middleware
app.use(cookieParser());
// Uses the session middleware to create a session
// whenever user accesses the website
app.use(session({
secret: 'test-secret',
resave: false,
saveUninitialized: true,
store: store,
}))
// Application will serve the html and css in the public directory
app.use(express.static("public"));
// Body Parser
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
// Routes
app.use('/spotify', require('./routes/Spotify'))
app.use('/session', require('./routes/Session'))
// View Engine
app.set('views', './views')
app.set('view engine', 'ejs');
// Start up the server
app.listen(PORT, () =>{
console.log("Welcome to the server");
})
module.exports = db;