-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
56 lines (46 loc) · 1.64 KB
/
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/* this is the server of website */
const express = require("express");
const multer = require("multer");
const Link = require("./link.js");
const app = express();
const upload = multer();
// Enable CORS (see https://enable-cors.org/server_expressjs.html)
app.use((req, res, next) => {
res.header("Access-Control-Allow-Origin", "*");
res.header(
"Access-Control-Allow-Headers",
"Origin, X-Requested-With, Content-Type, Accept"
);
next();
});
// Serve content of the "public" subfolder directly
app.use(express.static("public"));
// Initial links array
const links = [];
links.push(new Link("Hacker News", "https://news.ycombinator.com", "Baptiste"));
links.push(new Link("Reddit", "https://reddit.com", "Thomas"));
links.push(new Link("Boing Boing", "boingboing.net", "Daniel"));
// Routes
app.get("/", (request, response) => {
response.sendFile(`${__dirname}/views/index.html`);
});
app.get("/api/links", (request, response) => {
response.json(links);
});
app.post("/links", upload.array(), (request, response) => {
// Create new link object
console.log(request);
const title = request.body.title;
const url = request.body.url;
const author = request.body.author;
const link = new Link(title, url, author);
// Add new link to the beginning of the list
links.unshift(link);
// Send back newly created link as JSON
response.json(link);
});
// Start listening to incoming requests
// If process.env.PORT is not defined, port number 3000 is used
const listener = app.listen(process.env.PORT || 3000, () => {
console.log(`Your app is listening on port ${listener.address().port}`);
});