-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
70 lines (59 loc) · 1.99 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
const express = require('express');
const cors = require("cors");
const session = require("express-session");
const MongoStore = require("connect-mongo");
const fetch = require("node-fetch");
require('dotenv').config()
const passport = require("./server/passport/setup")
const PORT = process.env.PORT || 8080;
// Sets up the Express App
const app = express();
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
var corsOptions = {
origin: `http://localhost:${PORT}`
};
app.use(cors(corsOptions));
app.use(express.static('assets'));
const htmlRouter = require('./assets/Routes/htmlRoutes.js');
const apiRouter = require('./assets/Routes/APIRoutes.js');
const db = require("./models").db;
db.mongoose.connect(process.env.MONGODB_URI, {
useNewUrlParser: true,
useFindAndModify: false,
useUnifiedTopology:true
}).then(() => console.log(`Successfully connect to MongoDB 👍`))
.catch(err => {
console.error("Connection error", err);
process.exit();
});;
// Express Session
app.use(
session({
secret: "very secret this is",
resave:false,
saveUninitialized: true,
store: MongoStore.create({mongoUrl: process.env.MONGODB_URI})
})
)
// Passport middleware
app.use(passport.initialize())
app.use(passport.session())
// Invoke routes
apiRouter(app);
htmlRouter(app);
// app.use("/api/auth", auth);
// app.get("/api/goodMorning", (req, res) => res.send("Good morning"));
// Syncing our sequelize models and then starting our Express app
app.listen(PORT, () => console.log(`Listening on localhost:${PORT}`));
function updateItemPrices() {
const itemInterval = setInterval(function() {
console.log('Fetching Item Prices')
let requests = [fetch(process.env.baseURI || `http://localhost:${PORT}` + '/api/updateItemPrices').catch(err => console.log(err))]
// Promise.all waits until all jobs are resolved
Promise.all(requests)
.then(console.log('Item Price Data Updated Successfully'))
.catch(err => console.log(err))
}, 60000);
}
updateItemPrices();