-
Notifications
You must be signed in to change notification settings - Fork 0
/
3.js
72 lines (60 loc) · 2.03 KB
/
3.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
65
66
67
68
69
70
71
const fs = require("fs");
const express = require("express");
// 3 rd party midleware - logger
const morgan = require("morgan");
let htmlPage = fs.readFileSync("index.html", "utf-8");
const data = JSON.parse(fs.readFileSync("data.json", "utf-8"));
// server created
const server = express();
// code for server
server.use(morgan("dev")); //GET /public.html 304 3.294 ms - -
server.use(morgan("default")); //::1 - - [Mon, 24 Jul 2023 00:59:17 GMT] "GET /public.html HTTP/1.1" 304 - "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36"
// midleware - logger
// server.use((req, res, next) => {
// console.log(req.method, req.ip, req.hostname, new Date());
// // GET ::1 localhost 2023-07-23T14:38:07.206Z
// next();
// });
// body parser
server.use(express.json());
// for static hosting
server.use(express.static("public"));
const auth = (req, res, next) => {
// http://localhost:8080/?password=123->req.query
// if (req.body.password == 123) {
next();
// } else res.sendStatus(401);
};
// auth -middleware applies to whole server ,not to be done like this
// server.use(auth)
// api -endpoint -route
// testing in postman
server.post("/", auth, (req, res) => {
res.json({ type: "POST" });
});
server.delete("/", (req, res) => {
res.json({ type: "delete" });
});
server.put("/", (req, res) => {
res.json({ type: "put" });
});
server.patch("/", (req, res) => {
res.json({ type: "patch" });
});
server.get("/product/:id", auth, (req, res) => {
// id is dynamic
console.log(req.params)//{ id: '6' }
res.send("hello");
// Send a response.
// Examples:
// res.send({ some: 'json' });
// res.send('<p>some html</p>');
// res.status(404).send('Sorry, cant find that');
// res.sendFile("C:/Users/akhilesh upadhyay/Pictures/Screenshots/Screenshot (45).png")
// res.sendStatus(404)
// res.sendStatus(201).send('<h1>hello</h1>')
});
// listen server
server.listen(8080);
// 3 ways to send data from clint to server
// query parameters , body , url parameters