-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
56 lines (48 loc) · 1.37 KB
/
app.js
File metadata and controls
56 lines (48 loc) · 1.37 KB
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
// Dependencies
const express = require("express");
const expressLayouts = require("express-ejs-layouts");
const path = require("path");
require("dotenv").config();
const app = express();
// Parser middleware
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
// View engine
app.use(express.static(path.join(__dirname, "src/public")));
app.use(expressLayouts);
app.set("view engine", "ejs");
app.set("views", path.join(__dirname, "src/views"));
app.set("layout", "layout");
// Routes
const indexRouter = require("./src/routes/index");
const searchRouter = require("./src/routes/search");
const trackRouter = require("./src/routes/track");
const contactRouter = require("./src/routes/contact");
const aboutRouter = require("./src/routes/about");
app.use("/", indexRouter);
app.use("/search", searchRouter);
app.use("/track", trackRouter);
app.use("/contact", contactRouter);
app.use("/about", aboutRouter);
// Error handler
app.use((req, res, next) => {
res.status(404).json({
success: false,
error: "Not found",
});
});
app.use((err, req, res, next) => {
const status = err.status || 500;
res.status(status).json({
success: false,
error: err.message,
});
});
// Server
if (require.main === module) {
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}.`);
});
}
module.exports = app;