-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
28 lines (28 loc) · 967 Bytes
/
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
const tracer = require("./tracing")("todo-service");
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);
});