-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
62 lines (40 loc) · 1.41 KB
/
index.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
const { MongoClient, ServerApiVersion } = require('mongodb');
const express = require('express');
const cors = require('cors');
require('dotenv').config();
const app = express();
const port = process.env.PORT || 5000;
//new name: alumniUser
//new pass: YbZPUp1nHsUQp7rS
//use middleware
app.use(cors());
app.use(express.json());
const uri = `mongodb+srv://${process.env.DB_USER}:${process.env.DB_PASS}@cluster0.wzdwn29.mongodb.net/?retryWrites=true&w=majority`;
const client = new MongoClient(uri, { useNewUrlParser: true, useUnifiedTopology: true, serverApi: ServerApiVersion.v1 });
async function run(){
try{
await client.connect();
const studentCollection = client.db('alumniRegistration').collection('dataCollection');
app.get('/students', async(req , res) => {
const query = {};
const cursor = studentCollection.find(query);
const students = await cursor.toArray();
res.send(students);
});
//post
app.post('/students' , async(req, res) =>{
const newStudent = req.body;
const result = await studentCollection.insertOne(newStudent);
res.send(result);
})
}
finally{
}
}
run().catch(console.dir);
app.get('/', (req, res) => {
res.send('alumni Server Running');
});
app.listen(port, () =>{
console.log(`Server is running on port ${port}`);
});