forked from MarceloFonseca/tasks-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.js
35 lines (30 loc) · 1.13 KB
/
api.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
const express = require("express"),
bodyParser = require('body-parser'),
cors = require('cors'),
app = express(),
port = process.env.PORT || 8080,
newTaskPeriod = process.env.NEW_TASK_PERIOD || 60000;
app.use(cors({origin: '*'}));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
class Task {
constructor({id, text, completed = false}){
this.id = id;
this.text = text;
this.completed = completed;
}
}
const storage = new Map([[0, new Task({id: 0, text: "Task", completed: false})]]);
/**
* API get() Method
* Invoke it by accessing: http://localhost:PORT/api/tasks/
*/
app.get("/api/tasks/", (_, res) => res.json(Array.from(storage.values())));
/**
* A new Task will be added each minute if no period specified (e.g. NEW_TASK_PERIOD=60000)
*/
app.listen(port, () => {
setInterval(() => storage.set(storage.size, new Task({id: storage.size, text: `Task ${storage.size}`, completed: !!Math.round(Math.random())})), newTaskPeriod);
console.log(`Tasks Service listening on port ${port}`);
console.log(`A new task will be added every ${newTaskPeriod} ms`);
});