-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
64 lines (52 loc) · 1.81 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
63
64
var Express = require('express');
var Mongoclient=require('mongodb').MongoClient;
var cors=require('cors');
const multer=require('multer');
var app = Express();
app.use(cors());
var CONNECTION_STRING="mongodb+srv://admin:admin@cluster0.uyzsxc1.mongodb.net/?retryWrites=true&w=majority&appName=Cluster0";
var DATABASENAME="todoappdb";
var database;
const { MongoClient, ServerApiVersion } = require('mongodb');
const uri = "mongodb+srv://admin:admin@cluster0.uyzsxc1.mongodb.net/?retryWrites=true&w=majority&appName=Cluster0";
// Create a MongoClient with a MongoClientOptions object to set the Stable API version
const client = new MongoClient(uri, {
serverApi: {
version: ServerApiVersion.v1,
strict: true,
deprecationErrors: true,
}
});
async function run() {
try {
// Connect the client to the server (optional starting in v4.7)
await client.connect();
// Send a ping to confirm a successful connection
await client.db("admin").command({ ping: 1 });
console.log("Pinged your deployment. You successfully connected to MongoDB!");
} finally {
// Ensures that the client will close when you finish/error
await client.close();
}
}
run().catch(console.dir);
app.get('/api/todoapp/GetNotes',(req,res)=>{
database.collection("todoappcollection").find({}).toArray((err,result)=>{
if(err) throw err;
res.send(result);
})
})
app.post('/api/todoapp/AddNote',multer().none(),(req,res)=>{
database.collection("todoappcollection").count({}, function(err, result){
if (err) {
res.status(500).send(err);
} else {
res.status(200).send(result.ops[0]);
}
database.collection("todoappcollection").insertOne({
id:(numofDocs+1).toString(),
description:request.body.newNotes
});
response.json("Note added successfully");
})
});