-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
32 lines (27 loc) · 975 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
29
30
31
32
// Import express and axios
import express from 'express';
import axios from 'axios';
// Create an express app and set the port number.
const app = express();
const port = 3000;
// Use the public folder for static files.
app.use(express.static("public"));
// When the user goes to the home page it should render the index.ejs file.
app.get("/", async (req, res) => {
try {
const result = await axios.get("https://secrets-api.appbrewery.com/random");
// Use axios to get a random secret and pass it to index.ejs to display the
// secret and the username of the secret.
res.render("index.ejs", {
secret: result.data.secret,
user: result.data.username,
});
} catch (error) {
console.log(error.response.data);
res.status(500);
}
});
// Listen on your predefined port and start the server.
app.listen(port, () => {
console.log(`server is running on port ${port}`);
});