-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
84 lines (70 loc) · 1.89 KB
/
index.ts
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
import cookieParser from "cookie-parser";
import { config } from "dotenv";
import express from "express";
import "express-async-errors";
import { rateLimit } from "express-rate-limit";
import path from "path";
import logger from "./config/winston.config";
import connectDB from "./db/db.config";
import CustomError from "./errors/CustomError";
import errorThrower from "./errors/errorThrower";
import { ErrorResponse } from "./interfaces/errorResponseInterface";
import authRoutes from "./routes/auth.routes";
config();
const app = express();
const port = process.argv[3] || process.env.PORT || 3000;
app.use(express.static(path.join('public')))
.use(express.json())
.use(cookieParser())
.use(rateLimit({
windowMs: 2 * 60 * 1000,
limit: 10,
standardHeaders: true
}))
.set('views', path.join('views'))
.set('view engine', 'ejs');
//make a logger in general
app.use((req, res, next) =>
{
logger.info(`${req.baseUrl} :: ${req.hostname} :: ${req.url} :: ${req.subdomains}`);
next();
});
app.get('/', (req, res) =>
{
res.render('index');
});
app.use('/auth', authRoutes)
//page not found - error - has to be second last route
app.use("*", (req, res) =>
{
throw errorThrower(404, "Page Not Found!")
});
//error handler
app.use((err: CustomError, req, res, next) =>
{
//only log the internal server errors
if (err.statusCode >= 500)
{
logger.error(`${req.baseUrl} :: ${req.hostname} :: ${req.url} :: ${err.statusMessage} :: ${err.message} `)
}
//!send the error to the user
const errObj: ErrorResponse = {
message: err.message,
}
if (process.env.NODE_ENV === "dev")
{
errObj.cause = err?.cause;
errObj.stack = err?.stack
}
//!return with response
res.status(err.statusCode).send(errObj);
})
async function main()
{
await connectDB(process.env.DB_URI);
app.listen(port, () =>
{
console.log(`Listening on http://localhost:${port}`);
});
}
main();