-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
49 lines (42 loc) · 1.35 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
var express = require('express');
var app = express()
require('dotenv').config();
var MongoClient = require('mongodb').MongoClient;
var url = "mongodb+srv://jay:<password>@mongocluster-izqv5.mongodb.net/mongojk?retryWrites=true&w=majority";
const port = process.env.PORT || 3000;
MongoClient.connect(url, function(err, db) {
if (err) throw err;
var dbo = db.db("mongojk");
dbo.createCollection("jk", function(err, res) {
if (err) throw err;
console.log("Collection created!");
db.close();
});
});
app.listen(port, () => {
console.log(`Starting server at ${port}`);
});
app.get("/", (req, res) => {
MongoClient.connect(url, function(err, db) {
if (err) throw err;
var dbo = db.db("mongojk");
var myobj = { name: "Company Inc", address: "Highway 37" };
dbo.collection("jk").insertOne(myobj, function(err, res) {
if (err) throw err;
console.log("1 document inserted");
db.close();
});
});
res.send("hi Jay kukadiya ");
});
app.get('/getAll', (req, res) => {
MongoClient.connect(url, function(err, db) {
if (err) throw err;
var dbo = db.db("mongojk");
dbo.collection("jk").find({}).toArray(function(err, result) {
if (err) throw err;
res.send(result);
db.close();
});
});
});