forked from shradha-khapra/docker-testapp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
42 lines (33 loc) · 1.11 KB
/
server.js
File metadata and controls
42 lines (33 loc) · 1.11 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
const express = require("express");
const app = express();
const path = require("path");
const MongoClient = require("mongodb").MongoClient;
const PORT = 5050;
app.use(express.urlencoded({ extended: true }));
app.use(express.static("public"));
const MONGO_URL = "mongodb://admin:qwerty@localhost:27017";
const client = new MongoClient(MONGO_URL);
//GET all users
app.get("/getUsers", async (req, res) => {
await client.connect(URL);
console.log('Connected successfully to server');
const db = client.db("apnacollege-db");
const data = await db.collection('users').find({}).toArray();
client.close();
res.send(data);
});
//POST new user
app.post("/addUser", async (req, res) => {
const userObj = req.body;
console.log(req.body);
await client.connect(URL);
console.log('Connected successfully to server');
const db = client.db("apnacollege-db");
const data = await db.collection('users').insertOne(userObj);
console.log(data);
console.log("data inserted in DB");
client.close();
});
app.listen(PORT, () => {
console.log(`server running on port ${PORT}`);
});