Skip to content

Commit 3656538

Browse files
After making initial routes and connecting to db.
1 parent 4db44d8 commit 3656538

File tree

7 files changed

+77
-1
lines changed

7 files changed

+77
-1
lines changed

app.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
require("dotenv").config();
2+
const express = require("express");
3+
const notFoundMiddleware = require("./middleware/not-found");
4+
const errorMiddleware = require("./middleware/error-handler");
5+
const connectToDb = require("./db/connect");
6+
const req = require("express/lib/request");
7+
const router = require("./routes/products");
8+
const PORT = process.env.PORT || 3000;
9+
10+
const app = express();
11+
12+
// middlewares
13+
app.use(express.json());
14+
15+
// routes
16+
app.get("/", (req, res) => {
17+
res.send("Waheguru!");
18+
});
19+
20+
// product routes
21+
22+
app.use("/api/v1/products", router);
23+
24+
app.use(notFoundMiddleware);
25+
app.use(errorMiddleware);
26+
27+
const start = async () => {
28+
try {
29+
await connectToDb(process.env.MONGO_URI);
30+
31+
console.log("Connected To MongoDb!");
32+
33+
app.listen(
34+
PORT,
35+
console.log(`Server Listening At http://localhost:${PORT}`)
36+
);
37+
} catch (error) {
38+
console.log(error);
39+
}
40+
};
41+
42+
start();

controllers/products.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
const getAllProducts = async (req, res) => {
2+
res.status(200).json({ msg: "Waheguru!" });
3+
};
4+
5+
module.exports = {
6+
getAllProducts,
7+
};

db/connect.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// mongodb+srv://jashan:[email protected]/products?retryWrites=true&w=majority
2+
const mongoose = require("mongoose");
3+
4+
const connectToDb = (url) => {
5+
return mongoose.connect(url);
6+
};
7+
8+
module.exports = connectToDb;

middleware/error-handler.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
const errorHandlerMiddleware = async (err, req, res, next) => {
2+
console.log(err);
3+
return res
4+
.status(500)
5+
.json({ msg: "Something went wrong, please try again" });
6+
};
7+
8+
module.exports = errorHandlerMiddleware;

middleware/not-found.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const notFound = (req, res) => res.status(404).send("Route does not exist");
2+
3+
module.exports = notFound;

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"description": "api which provides you fake store data.",
55
"main": "app.js",
66
"scripts": {
7-
"test": "echo \"Error: no test specified\" && exit 1"
7+
"start": "nodemon app.js"
88
},
99
"author": "Jashan Mago",
1010
"license": "ISC",

routes/products.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
const express = require("express");
2+
const router = express.Router();
3+
4+
const { getAllProducts } = require("../controllers/products");
5+
6+
router.route("/").get(getAllProducts);
7+
8+
module.exports = router;

0 commit comments

Comments
 (0)