-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
63 lines (48 loc) · 1.91 KB
/
app.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
57
58
59
60
61
62
63
const express = require('express');
const axios = require('axios');
const app = express();
// Set EJS as the templating engine
app.set('view engine', 'ejs');
// Serve static files from the 'public' folder
app.use(express.static('public'));
// Home route to render input form
app.get('/', (req, res) => {
res.render('index');
});
// Result route: fetch a joke and personalize it
app.get('/result', async (req, res) => {
const name = req.query.name || "Chuck Norris";
const category = req.query.category || "Any";
const jokeApiUrl = `https://v2.jokeapi.dev/joke/${category}`;
const factApiUrl = 'https://uselessfacts.jsph.pl/api/v2/facts/random?language=en';
try {
// Fetch the joke from JokeAPI
const jokeResponse = await axios.get(jokeApiUrl);
let joke = '';
if (jokeResponse.data.type === 'single') {
joke = jokeResponse.data.joke;
} else {
joke = `${jokeResponse.data.setup} ${jokeResponse.data.delivery}`;
}
const personalizedJoke = joke.replace(/Chuck Norris/g, name);
// Fetch a random fact from the updated Random Facts API
const factResponse = await axios.get(factApiUrl, {
headers: {
Accept: 'application/json'
}
});
// Log the response to see the fact
console.log(factResponse.data);
const randomFact = factResponse.data.text;
// Render the result page with the personalized joke and random fact
res.render('result', { name, personalizedJoke, randomFact });
} catch (error) {
console.error(error); // Log any errors that occur
res.render('error', { message: 'Unable to retrieve a joke or fact. Please try again.' });
}
});
// Start the server
const PORT = process.env.PORT || 3000;
app.listen(PORT, '0.0.0.0', () => {
console.log(`Server is running on http://0.0.0.0:${PORT}`);
});