-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
69 lines (62 loc) · 1.69 KB
/
index.js
File metadata and controls
69 lines (62 loc) · 1.69 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
const express = require("express"); // npm i express
const app = express();
const knex = require("./config/db");
app.use(express.json());
// home page
app.get("/", (req, res) => {
res.send('<h1 style="text-align: center;">Welcom my friend Welcom</h1>');
});
// sinup
app.post("/sinup", async (req, res) => {
const { name, email, password } = req.body;
try {
if (!name && !email && !password) {
return res.send("fill all the fields");
}
const user = await knex("Test").where({ email: req.body.email });
if (user.length == 0) {
await knex("Test").insert(req.body);
return res.send("Data Inserted");
}
res.send("User already exists");
} catch (error) {
res.send(error.message);
}
});
//login
app.get("/login", async (req, res) => {
const { email, password } = req.body;
if (!email && !password) {
return res.send("please enter data");
}
const msg = await knex("Test").where({
email: req.body.email,
password: req.body.password,
});
if (msg.length == 0) {
return res.send("something went wrong ");
}
res.send("logged in successfully");
});
//delete
app.delete("/delete", async (req, res) => {
const { email, password } = req.body;
if (!email && !password) {
return res.send("please enter data");
}
await knex("Test").where({ email, password }).del();
res.send("deleted successfully");
});
// update
app.patch("/update/:id", async (req, res) => {
const email = req.body;
const id = req.params.id;
if (!email) {
return res.send("please enter a email");
}
await knex("Test").where({ id }).update({ email });
res.send("Email updated successfully");
});
app.listen(5000, () => {
console.log("Conncted");
});