-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
47 lines (37 loc) · 1.07 KB
/
server.js
File metadata and controls
47 lines (37 loc) · 1.07 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
import express from "express";
import mongoose from "mongoose";
import bodyParser from "express";
import userRouter from "./Routes/user.js";
import contactRouter from "./Routes/contact.js";
import { config } from "dotenv";
//To secure the port, Database string uri we use .env and these variable in .env file
// For that npm i dotenv and install npm i cors, cors to not get error of cors to frontend developers
// add .env file in .gitignore
//.env setup
config({path:'.env'})
const app = express();
const port = process.env.PORT;
app.use(express.urlencoded({ extended: true }));
app.use(bodyParser.json());
//user routes
app.use("/api/user", userRouter);
//contact router
app.use("/api/contact", contactRouter);
//Home route
app.get("/", (req, res) => {
res.json({ message: "This is home route" });
});
mongoose
.connect(
process.env.MONGO_URI,
{ dbName: "Nodejs_Mastery_Course" }
)
.then(() => {
console.log("MongoDB connected...");
})
.catch((err) => {
console.log(err);
});
app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});