-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.ts
80 lines (62 loc) · 2.25 KB
/
app.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
import { userRouter } from "./src/user/user-routes/user-routes";
import express, { Request, Response, Application } from "express";
import * as fs from "fs";
import * as path from "path";
import imageRouter from "./src/Image/image-routes/image-routes";
import querystring from "querystring";
const mainRouter = express.Router();
const bodyParser = require("body-parser");
const mongoose = require("mongoose");
const app: Application = express();
const PORT = process.env.PORT || 8000;
//Here we are configuring express to use our middleware
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use("/image", imageRouter);
app.use("/", mainRouter);
app.use("/user", userRouter);
const password = process.env.DATABASE_PASSWORD;
const localDB = process.env.DATABASE_LOCAL;
let db;
if (password != undefined) {
db = process.env.DATABASE?.replace("<PASSWORD>", password);
}
// Mongo Atlas Cloud Config
// let mongooseConnect = mongoose.connect("mongodb://localhost:27017");
// console.log(
// mongooseConnect.then((thread: any) => {
// // console.log('Mongoose Launch', thread)
// })
// );
let options = {
root: path.join(__dirname),
};
//this middleware is used for logging
app.use((req: any, res: any, next: any) => {
let request = req.method.toUpperCase() + " " + req.url;
if (Object.keys(req.query).length) {
request += "?" + querystring.stringify(req.query);
}
if (Object.keys(req.body).length) {
request += "\r\n" + JSON.stringify(req.body);
}
console.info(
`--------------------------------------------------------------\nRequest: ${request}`
);
//next() tells Express to find the next request handler
next();
//NOTES:
// #1 - you can put some logic here, but it should only handle global tasks like authentication and parsing formats
// #2 - you should not put your database logic here
});
// Serve HTML Pages
mainRouter.get("/", (req: Request, res: Response): void => {
res.sendFile(path.resolve("./public/index.html"));
});
mainRouter.get("/home", (req: Request, res: Response): void => {
console.log(path.resolve('./public/home.html'));
res.sendFile(path.resolve('./public/home.html'));
});
app.listen(PORT, (): void => {
console.log(`Server Running here 👉 https://localhost:${PORT}`);
});