-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path14_ExpressJS.js
More file actions
82 lines (67 loc) · 2.64 KB
/
14_ExpressJS.js
File metadata and controls
82 lines (67 loc) · 2.64 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
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
81
82
/*
https://expressjs.com/
->Express.js:
-Used to create servers in nodejs very easily i.e creating APIs easily
-It's is popular framework for building web applications and APIs using Node.js
install express.js using this command: npm i express
*/
//importing express js
const express = require('express')
//the blue print of express() is now inside 'app' its like instance of express
const app = express() //'app' can be any name but generally all developers use 'app'
/* ->app.get takes two parameters:
(app.get() is a method in Express used to define a route that listens for GET requests on a specific URL or endpoint.)
1)to which endpoint it needs to be shown (kon se endpoint pei ye show hona chahiye)
here endpoint is "/", i.e after address when the user types this "/", the user will get data
2)a callback function that handles the request and response (req, res)
req->request
res->response: what response you want to send here it's "Hello Express"
*/
//app.get(path, callback)
//localhost:3000 or localhost:3000/
app.get('/', (req, res) => {
// res.send("hello express") //if you use this as well then only this will be shown in response of tha api, it will return first res.send
res.send('Hello Welcome to my hotel!')
})
//another get request: localhost:3000/menu
app.get('/menu',(req,res)=>{
res.send('We currently have: Pizza, Burger, Egg Roles, Briyani, Butter Chicken, Tandori Chicken')
})
//localhost:3000/sunday_special
app.get('/sunday_special',(req,res)=>{
res.send('Sunday Special is: Chicken Pizza + 2 burgers @499 only')
})
//return object
app.get("/pizza",(req,res)=>{
const pizzaInfo={
name:"Chick Pizza",
size:"large",
cheeseTopping:true,
extraCheese:false,
}
res.send(pizzaInfo) //o/p will be json string
})
app.get("/fridaySpecial",(req,res)=>{
const biryani={
name:"Wazwan biryani",
size:"Large",
price:699,
extraRice:true,
}
res.send(biryani)
})
//3000 is port, telling that server is active on port 3000
// after running the code, go to web browser and type localhost:3000/
//app.listen(3000)
//or
app.listen(3000,()=>{
console.log("Server is listening on port 3000")
})
//you can also run this inside the postman : http://localhost:3000/ , http://localhost:3000/pizza , http://localhost:3000/menu
//sample POST Request
app.post('/order',(req,res)=>{
res.send("Order Received!")
})
//Now it this endpoint : http://localhost:3000/order but with POST Request and for now send any json data or empty data
//it will give "Order Received!" as o/p in postman
//Note: To run this file do this in terminal: node --watch 14_ExpressJS.js (--watch is optional)