-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
44 lines (36 loc) · 909 Bytes
/
index.js
File metadata and controls
44 lines (36 loc) · 909 Bytes
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
const express = require("express");
const { MongoClient } = require("mongodb");
const app = express();
app.use(express.json());
const port = 3000;
let db;
const startServer = async () => {
const client = await MongoClient.connect("mongodb://localhost:27017/");
db = client.db("todo");
await db.collection("todos").insertMany([
{
id: "1",
title: "Buy groceries",
},
{
id: "2",
title: "Install Aspecto",
},
{
id: "3",
title: "buy my own name domain",
},
]);
app.listen(port, () => {
console.log(`Example app listening on port ${port}`);
});
};
startServer();
app.get("/todo", async (req, res) => {
const todos = await db.collection("todos").find({}).toArray();
res.send(todos);
});
app.get("/todo/:id", async (req, res) => {
const todo = await db.collection("todos").findOne({ id: req.params.id });
res.send(todo);
});