-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
56 lines (42 loc) · 1.24 KB
/
main.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
// const express = require("express");
// const app = express();
// const port = 3000;
// app.get("/", (req, res) => {
// res.send("welcome");
// });
// app.listen(port, () => {
// console.log("Server is running on port", port);
// });
//----------------------------------------------------------------
// const express = require("express");
// const app = express();
// const port = 3000;
// app.get("/", (req, res) => {
// const { name, age } = req.query;
// res.send(`${name},${age}`);
// });
// app.listen(port, () => {
// console.log("Server is running on port", port);
// });
//----------------------------------------------------------------
const express = require("express");
const app = express();
const port = 3000;
// Middleware to parse JSON requests
app.use(express.json());
app.get("/", (req, res) => {
const { name, age } = req.query;
res.send(`${name}, ${age}`);
});
app.post("/contact", (req, res) => {
const { name, phone, email } = req.body;
if (!name) {
return res.status(422).json({ error: "Name is required." });
}
res.status(200).json({
message: `Contact details for ${name} received as ${phone} and ${email}`,
});
});
app.listen(port, () => {
console.log("Server is running on port", port);
});