-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
71 lines (57 loc) · 1.79 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
69
70
71
import "dotenv/config"
import path from "node:path"
import cookieParser from "cookie-parser"
import express from "express"
import createError from "http-errors"
import helmet from "helmet"
import compression from "compression"
import { StatusCodes } from "http-status-codes"
import mongoose from "mongoose"
import morgan from "morgan"
import catalogRouter from "./routes/catalog.js"
import indexRouter from "./routes/index.js"
import usersRouter from "./routes/users.js"
const app = express()
const mongoUrl = process.env.MONGO
const main = async () => {
await mongoose.connect(mongoUrl)
console.log("Connected to MongoDB")
}
const dirname = path.dirname(new URL(import.meta.url).pathname)
mongoose.set("strictQuery", false)
main().catch(console.error)
// view engine setup
app.set("views", path.join(dirname, "views"))
app.set("view engine", "pug")
app.use(morgan("dev"))
app.use(compression())
app.use(
helmet.contentSecurityPolicy({
directives: {
"script-src": ["'self'", "code.jquery.com", "cdn.jsdelivr.net"],
},
}),
);
app.use(express.json())
app.use(express.urlencoded({ extended: false }))
app.use(cookieParser())
app.use(express.static(path.join(dirname, "public")))
app.use("/", indexRouter)
app.use("/users", usersRouter)
app.use("/catalog", catalogRouter)
// catch 404 and forward to error handler
app.use((_req, _res, next) => {
next(createError(StatusCodes.NOT_FOUND))
})
// error handler
app.use((error, req, res, _next) => {
// set locals, only providing error in development
res.locals.message = error.message
res.locals.error = req.app.get("env") === "development" ? error : {}
// render the error page
const serverError = 500
res.status(error.status || serverError)
res.render("error")
})
app.listen(3000)
console.log("Server running on port 3000")