-
Notifications
You must be signed in to change notification settings - Fork 6
/
server.js
136 lines (114 loc) · 4.59 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
135
136
/*eslint no-console: ["error", { allow: ["warn", "error", "log"] }] */
// server.js
"use strict";
// BASE SETUP
// =============================================================================
// call the packages we need
const express = require("express"); // call express
const app = express(); // define our app using express
const bodyParser = require("body-parser");
const path = require("path");
const mongoose = require("mongoose");
const authController = require("./controllers/auth");
const mangaController = require("./controllers/manga");
const userController = require("./controllers/user");
require("dotenv").config({ silent: true });
app.set("superSecret", process.env.SECRET); // secret constiable
// configure app to use bodyParser()
// this will let us get the data from a POST
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
// REGISTER OUR ROUTES -------------------------------
const router = express.Router(); // get an instance of the express Router
app.use((req, res, next) => {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Methods", "GET,PUT,POST,DELETE");
res.header("Access-Control-Allow-Headers", "x-access-token");
next();
});
// all of our routes will be prefixed with /api/v#
app.use(`/api/${process.env.API_VERSION}`, router);
// Serve Swagger UI at https://mangadbv2.herokuapp.com
app.use("/", express.static(path.join(__dirname, "docs")));
//(accessed at GET https://mangadbv2.herokuapp.com/api/v#)
router.route("/").get(mangaController.getWelcome);
// switching default mongoose promises to global object's promises
mongoose.Promise = global.Promise;
// Connect to the database
const mongouri =
process.env.MONGOLAB_URI || `mongodb://${process.env.IP}:27017/mangadb`;
mongoose.connect(mongouri, {
useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true
});
var db = mongoose.connection;
db.on("error", console.error.bind(console, "connection error:"));
db.once("open", function() {
// we're connected!
console.log(`Connected to ${mongouri}`);
});
//Create endpoint handlers for /mangas/:username/:id
router
.route("/mangas/:username/:id")
// get user's manga info
.get(authController.validateToken, mangaController.getManga)
// update user's manga info
.put(authController.validateToken, mangaController.putManga)
// deletes user's manga
.delete(authController.validateToken, mangaController.delManga);
//Create endpoint handlers for /mangas/:user/title/:manga_title
router
.route("/mangas/:username/title/:manga_title")
// get user's manga info
.get(authController.validateToken, mangaController.getManga)
// update user's manga info
.put(authController.validateToken, mangaController.putManga)
// deletes user's manga
.delete(authController.validateToken, mangaController.delManga);
// Create endpoint handlers for /mangas/:username
router
.route("/mangas/:username")
//get all user's manga
.get(authController.validateToken, mangaController.getMangas)
//create new manga
.post(authController.validateToken, mangaController.postManga)
// Deletes all user mangas
.delete(authController.validateToken, mangaController.delUserMangas);
// Get all mangas by admin
router
.route("/mangas")
//admin get all mangas
.get(authController.validateToken, mangaController.getAllMangas)
// admin delete all mangas
.delete(authController.validateToken, mangaController.delMangas);
// HANDLE USER RELATED ROUTES
// ===========================================================================
// Request token generator at /auth
// Get Json web token authentication
router.route("/auth").post(authController.generateToken);
// Request a new password at /reset
router.route("/reset").post(authController.generateOTP);
// Create endpoint handlers for /users
router
.route("/users")
// Creates new user
.post(userController.postUsers)
//admin get all users
.get(authController.validateToken, userController.getUsers)
//admin delete all users
.delete(authController.validateToken, userController.delUsers);
//Create endpoint handlers for /mangas/:username
router
.route("/users/:username")
// get user info
.get(authController.validateToken, userController.getUser)
// update user info
.put(authController.validateToken, userController.putUser)
// deletes user
.delete(authController.validateToken, userController.delUser);
// CONFIGURE & START THE SERVER
// =============================================================================
const port = process.env.PORT || 8080; // set our port
app.listen(port, () => console.log(`Node.js listening on port ${port}`));
module.exports = app;