forked from heckman-codes/Project-2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
83 lines (65 loc) · 2.03 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
77
78
79
80
81
82
83
// *****************************************************************************
// Server.js - This file is the initial starting point for the Node/Express server.
//
// ******************************************************************************
// *** Dependencies
// =============================================================
const dotenv = require('dotenv');
dotenv.config();
const path = require("path");
var express = require("express");
var exphbs = require("express-handlebars");
const sequelize = require("./config/config")
var db = require("./models")
var app = express();
const cookieParser = require('cookie-parser');
const jwt = require('jsonwebtoken');
// Sets up the Express App
// =============================================================
var app = express();
var PORT = process.env.PORT || 3005;
// Requiring our models for syncing
var db = require('./models');
app.engine(
"handlebars",
exphbs({
defaultLayout: "main"
})
);
app.set("view engine", "handlebars");
// Middleware
app.use(express.urlencoded({ extended: false }));
app.use(express.json());
app.use(express.static(path.join(__dirname, "public")));
app.use(cookieParser());
// Static directory
app.use(express.static('public'));
app.use((req, res, next) => {
const token = req.cookies.token;
if (token) {
const { id } = jwt.verify(token, process.env.APP_SECRET);
req.user = id;
}
next();
});
// Routes
// =============================================================
require('./routes/apiRoutes')(app);
require('./routes/htmlRoutes')(app);
var syncOptions = { force: false };
// If running a test, set syncOptions.force to true
// clearing the `testdb`
if (process.env.NODE_ENV === "test") {
syncOptions.force = true;
}
// Starting the server, syncing our models ------------------------------------/
db.sequelize.sync(syncOptions).then(function () {
app.listen(PORT, function () {
console.log(
"==> 🌎 Listening on port %s. Visit http://localhost:%s/ in your browser.",
PORT,
PORT
);
});
});
module.exports = app;