Skip to content

Commit 192ab3f

Browse files
author
sourabh
committed
create - sever production api & route
1 parent 70b2da0 commit 192ab3f

File tree

6 files changed

+43
-1
lines changed

6 files changed

+43
-1
lines changed

.env

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
PORT = 4000

app.js

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
require("dotenv").config();
2+
const express = require('express')
3+
const app = express()
4+
5+
// Ref: ✈️🔗https://expressjs.com/en/5x/api.html#router
6+
7+
// BRINGS ROUTES
8+
const home = require("./routes/home");
9+
10+
// USING MIDDLEWARE
11+
app.use("/api/v1",home);
12+
13+
module.exports = app;

controllers/home.js

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
exports.home = (req,res)=>{
2+
res.status(200).json({
3+
success:true,
4+
gretting:"Hello,Home Page From API"
5+
})
6+
}
7+
exports.Dummy = (req,res)=>{
8+
res.status(200).json({
9+
success:true,
10+
gretting:"Hello,Dummy From API"
11+
})
12+
}

index.js

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
require("dotenv").config();
2+
const app = require("./app")
3+
4+
app.listen(process.env.PORT, () => {
5+
console.log(`Example app listening on port http://localhost:${process.env.PORT}/api/v1`)
6+
})

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
"description": "a backend api for tshirt store",
55
"main": "index.js",
66
"scripts": {
7-
"test": "echo \"Error: no test specified\" && exit 1"
7+
"start": "nodemon index.js",
8+
"dev": "node index.js"
89
},
910
"keywords": [
1011
"api",

routes/home.js

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const express = require('express');
2+
const {home, Dummy } = require( '../controllers/home' );
3+
const router = express.Router();
4+
5+
// router.get("/",home)
6+
router.route("/").get(home)
7+
router.route("/d").get(Dummy)
8+
9+
module.exports = router;

0 commit comments

Comments
 (0)