generated from render-examples/express-hello-world
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
34 lines (27 loc) · 815 Bytes
/
app.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
const express = require("express");
const app = express();
const port = process.env.PORT || 3001;
require("dotenv").config();
const { MongoClient } = require("mongodb");
const mongoClient = new MongoClient(process.env.MONGO_DB_CONNECTION_STRING);
let testCollection;
mongoClient.connect().then(_ => {
const db = mongoClient.db("test");
testCollection = db.collection("test");
}).catch(error => {
console.log(error);
})
app.use(express.json());
app.get("/api/test", (_, response) => {
testCollection.find().toArray().then((result) => {
response.json(result);
});
});
app.post("/api/test", (request, response) => {
testCollection.insertOne(request.body).then((_) => {
response.json();
});
});
app.listen(port, () => {
console.log(`listening on port ${port}`)
})