-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.js
56 lines (41 loc) · 1.69 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
const express = require("express");
const server = express();
const staticHandler = express.static("public");
const addCatPage = require("./src/handler/addCat");
const catPage = require("./src/handler/catPage");
const homePage = require("./src/handler/home");
const logInPage = require("./src/handler/logIn");
const logOutPage = require("./src/handler/logOut");
const signUpPage = require("./src/handler/signUp");
const checkAuth = require("./src/middleware/checkAuth");
const catPic = require("./src/handler/catPic");
// middleware - gets cookie header, parses into obj + attaches to request
const cookieParser = require("cookie-parser");
const bodyParser = express.urlencoded({ extended: false });
const multer = require("multer");
const upload = multer();
const logger = require("./src/middleware/logger");
server.use(cookieParser(process.env.COOKIE_SECRET));
server.use(staticHandler);
server.use(logger.logger);
// routes
server.get("/", homePage.get);
server.get("/sign-up", signUpPage.get);
server.post("/sign-up", bodyParser, signUpPage.post);
server.get("/log-in", logInPage.get);
server.post("/log-in", bodyParser, logInPage.post);
server.post("/log-out", bodyParser, logOutPage.post);
server.get("/add-cat", checkAuth.checkAuth, addCatPage.get);
server.post("/add-cat", upload.single("cat_photo"), addCatPage.post);
server.get("/cat-pic/:catid", catPic.get);
server.get("/cats/:catid", catPage.get);
server.post('/cats/:catid', bodyParser, catPage.post);
const PORT = process.env.PORT || 3000;
//Error-handling fail safe
process.on("unhandledRejection", (error) => {
console.error(error);
process.exit(1);
});
server.listen(PORT, () => {
console.log(`Listening on http://localhost:${PORT}`);
});