forked from papoita/quizapp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
70 lines (60 loc) · 2.05 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
// REQUIREMENTS
// load .env data into process.env
require("dotenv").config();
//node nodules dependencies
const express = require("express");
const morgan = require("morgan");
const cookieSession = require('cookie-session')
const { Pool } = require("pg");
//const bodyParser = require('body-parser');
//local helper files
const sassMiddleware = require("./lib/sass-middleware");
const dbParams = require("./lib/db.js");
// Separated Routes for each Resource
const usersRoutes = require("./routes/users");
const quizzesRoutes = require("./routes/quizzes");
const questionsRoutes = require("./routes/questions");
const attemptsRoutes = require("./routes/attempts");
const viewsRoutes = require("./routes/views")
// SERVER SETTINGS
// Web server config
const app = express();
const PORT = process.env.PORT || 8080;
// PG database client/connection setup
const db = new Pool(dbParams);
db.connect();
// MIDDLEWARES
// Load the logger first so all (static) HTTP requests are logged to STDOUT
// 'dev' = Concise output colored by response status for development use.
// The :status token will be colored red for server error codes, yellow for client error codes, cyan for redirection codes, and uncolored for all other codes.
app.use(morgan("dev"));
app.use(express.urlencoded({ extended: true }));
app.use(cookieSession({
name: "session",
keys: ["paola", "perez"],
// Cookie Options
maxAge: 30 * 60 * 1000 // 24 hours
}))
app.use(
"/styles",
sassMiddleware({
source: __dirname + "/styles",
destination: __dirname + "/public/styles",
isSass: false, // false => scss, true => sass
})
);
app.use(express.static("public"));
app.set("view engine", "ejs");
// app.use(bodyParser.json());
// app.use(bodyParser.urlencoded({ extended: true }));
//ROUTES ENDPOINTS
app.use("/api/users", usersRoutes(db));
app.use("/api/quizzes", quizzesRoutes(db));
app.use("/api/questions", questionsRoutes(db));
app.use("/api/attempts", attemptsRoutes(db));
app.use("/", viewsRoutes(db));
//LISTENER
app.listen(PORT, () =>
{
console.log(`Example app listening on port ${PORT}`);
});