-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
68 lines (56 loc) · 1.7 KB
/
app.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
let express = require("express");
let app = express();
const mongoose = require("mongoose");
let config = require("./config/config");
let flash = require("connect-flash");
let session = require("express-session");
let cookieParser = require("cookie-parser");
let ticketsApi = require("./routes/tickets");
let attendeesApi = require("./routes/attendees");
let categoriesApi = require("./routes/categories");
mongoose
.connect(config.MONGODB_CONNECTION, {
useNewUrlParser: true,
useUnifiedTopology: true
})
.then(res => {
console.log("Database connection established successfully!");
});
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(
session({
cookie: { maxAge: 60000 },
secret: "woot",
resave: false,
saveUninitialized: false
})
);
app.use(flash());
// API endpoints
app.get("/", (req, res) => res.send("Welcome to Eventomatic API."));
app.use("/tickets/", ticketsApi);
app.use("/attendees/", attendeesApi);
app.use("/categories/", categoriesApi);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
// next(createError(404));
res
.status(404)
.json({ success: false, error: `No route found for ${req.url}` });
});
// error handler
app.use(function(err, req, res, next) {
// response and log errors
console.log(err);
res.status(err.status || 500);
res.json({ success: false, error: "Somthing is wrong!" });
});
const PORT = process.env.PORT || 5000;
app.listen(PORT, () => console.log(`Server is running on port ${PORT}...`));
// Handle UnhandeledExceptions
process.on("unhandledRejection", (err, promise) => {
console.log(`Error: ${err.message}`);
});
module.exports = app;