-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
79 lines (69 loc) · 2.38 KB
/
server.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
72
73
74
75
76
77
78
79
const express = require('express');
const app = express();
const mongoose = require('mongoose');
require("dotenv").config();
const cors = require('cors');
const Aadhar = require('./model');
// Connect to MongoDB
mongoose
.connect(process.env.MONGO_URI, {
useNewUrlParser: true,
useUnifiedTopology: true,
})
.then(() => console.log("Connected to MongoDB"))
.catch((err) => console.log("error connecting to MongoDB:", err));
app.use(express.json());
app.use(cors());
// Routes
app.get('/', (req, res) => {
res.json({ message: 'Hi! This is an aadhar database server by team CloakR' });
});
// Get single user by Aadhar No
app.get(`/get-info`, async (req, res) => {
const { aadharNo } = req.query;
if (!aadharNo) return res.status(400).json({ message: 'Please provide a valid Aadhar No' });
try {
const userData = await Aadhar.find({ aadharNo: aadharNo });
if (userData.length === 0) return res.status(404).json({ message: 'No user found with the given Aadhar No' });
return res.status(200).json({
message: `You requested info for Aadhar No: ${aadharNo}`,
data: userData
});
} catch (error) {
console.log(error);
return res.status(500).json({ message: 'Error fetching user data' });
}
});
// Get all users
app.get(`/get-all`, async (req, res) => {
try {
const allData = await Aadhar.find();
if (allData.length === 0) return res.status(404).json({ message: 'No user found' });
return res.status(200).json({
message: `All data fetched successfully`,
data: allData
});
} catch (error) {
console.log(error);
return res.status(500).json({ message: 'Error fetching user data' });
}
});
// Add new user to aadhar database
app.post('/add-info', async (req, res) => {
const { aadharNo, name, phoneNo, state } = req.body;
try {
const userData = await Aadhar.create({
aadharNo: aadharNo,
name: name,
phoneNo: phoneNo,
state: state
});
return res.status(200).json({ message: 'User added successfully', data: userData });
} catch (error) {
console.log(error);
return res.status(500).json({ message: 'Error adding user' });
}
});
app.listen(process.env.PORT, () => {
console.log(`Server started on port ${process.env.PORT}`);
});