-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
64 lines (56 loc) · 1.56 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
const express = require("express");
const mongoose = require("mongoose");
const dotenv = require("dotenv");
const cookieParser = require("cookie-parser");
const path = require("path");
//
//
const userRoutes = require("./routes/userRoutes");
const postRoutes = require("./routes/postRoutes");
//.
const imageRoutes = require("./routes/imageRoutes");
//
const app = express();
app.use(express.json());
//
dotenv.config();
//middlewares- of recently installed npm's
app.use(cookieParser());
// view engine
app.set("view engine", "ejs");
//
//this line was vital to show image on frontend
//this is for absolute path
// app.use("/images", express.static(path.join(__dirname, "images")));
//this line was vital to show image on frontend
//this is for relative path
app.use("/images", express.static("./images"));
//mongoDB cloud
let uri = `mongodb+srv://${process.env.DB_USERNAME}:${process.env.DB_PASSWORD}@cluster0.te788iv.mongodb.net/socialApp-15-may?retryWrites=true&w=majority`;
//
mongoose.connect(uri, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
//home page would redirect to login url
app.get("/", (req, res) => {
try {
res.redirect("/user/register");
} catch (err) {
console.log(err);
res.json(err);
}
});
//
app.use("/user", userRoutes);
app.use("/post", postRoutes);
//-
app.use("/image", imageRoutes);
//
app.use((req, res, next) => {
res.status(404).render("404"); // Assuming you have a view called "404.ejs" for the error page
});
const port = process.env.PORT || 3000;
app.listen(port, () => {
console.log(`Server Started at ${port}`);
});