-
Notifications
You must be signed in to change notification settings - Fork 0
/
serverf.js
33 lines (32 loc) · 871 Bytes
/
serverf.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
// this uses "fetch" to get data from a url
var express = require("express");
var app = express();
const fetch = require("node-fetch");
app.use(express.json());
// store contacts in an arrays
var contacts = [];
const getData = async () => {
let url = "https://pollysnips.s3.amazonaws.com/users.json";
try {
let res = await fetch(url);
contacts = await res.json();
console.log(contacts);
} catch (error) {
console.log("error");
}
};
getData();
app.get("/", function (req, res) {
res.send("<h1> Routes: try POST to /contact and GET /contacts </h1>");
});
// list all contacts
app.get("/contacts", function (req, res) {
res.json(contacts);
});
// add a contact
app.post("/contact", (req, res) => {
contacts.push({ name: req.body.name, email: req.body.email });
res.json(req.body);
});
app.listen(3000);
console.log("Running on port 3000");