generated from lighthouse-labs/node-skeleton
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
134 lines (121 loc) · 3.5 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// load .env data into process.env
require("dotenv").config();
// Web server config
const PORT = process.env.PORT || 8080;
const ENV = process.env.ENV || "development";
const express = require("express");
const bodyParser = require("body-parser");
const sass = require("node-sass-middleware");
const app = express();
const morgan = require("morgan");
const cookieSession = require("cookie-session");
const uuidv1 = require("uuid/v1");
// PG database client/connection setup
const { Pool } = require("pg");
const dbParams = require("./lib/db.js");
const db = new Pool(dbParams);
db.connect();
app.use(
cookieSession({
name: "user_id2",
keys: [uuidv1()]
})
);
app.use(
cookieSession({
name: "latlng",
keys: [uuidv1()]
})
);
// 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.set("view engine", "ejs");
app.use(bodyParser.urlencoded({ extended: true }));
app.use(
"/styles",
sass({
src: __dirname + "/styles",
dest: __dirname + "/public/styles",
debug: true,
outputStyle: "expanded"
})
);
app.use(express.static("public"));
// Separated Routes for each Resource
// Note: Feel free to replace the example routes below with your own
const usersRoutes = require("./routes/users");
// const widgetsRoutes = require("./routes/widgets");
const indexRoutes = require("./routes/index");
const createRoutes = require("./routes/create");
const mapIdRoutes = require("./routes/maps");
const loginRoutes = require("./routes/login");
const registerRoutes = require("./routes/register");
const userMapRoutes = require("./routes/userMap");
const userFavMapRoutes = require("./routes/userFavMap");
const userContMapRoutes = require("./routes/userContMap");
const logoutRoutes = require("./routes/logout");
const flagEditRoutes = require("./routes/flagEdit");
//Importing helper file
const mapCreator = require("./helpers");
// Mount all resource routes
// Note: Feel free to replace the example routes below with your own
app.use("/api/users", usersRoutes(db));
// app.use("/api/widgets", widgetsRoutes(db));
app.use("/create", createRoutes(db));
app.use(
"/maps/:map_id",
function(req, res, next) {
req.map_config = {
map_id: req.params.map_id
};
next();
},
mapIdRoutes(db)
);
app.use("/flagEdit", flagEditRoutes(db));
app.use("/login", loginRoutes(db));
app.use("/logout", logoutRoutes(db));
app.use("/register", registerRoutes(db));
app.use(
"/user/:user_id",
function(req, res, next) {
req.user_config = {
user_id: req.params.user_id
};
next();
},
userMapRoutes(db)
);
app.use(
"/user/:user_id/favorites",
function(req, res, next) {
req.user_config = {
user_id: req.params.user_id
};
next();
},
userFavMapRoutes(db)
);
app.use(
"/user/:user_id/contributions",
function(req, res, next) {
req.user_config = {
user_id: req.params.user_id
};
next();
},
userContMapRoutes(db)
);
// Note: mount other resources here, using the same pattern above
// Home page
// Warning: avoid creating more routes in this file!
// Separate them into separate routes files (see above).
app.use("/", indexRoutes(db));
app.get("*", (req, res) => {
res.render("catchAll");
});
app.listen(PORT, () => {
console.log(`Example app listening on port ${PORT}`);
});