-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
29 lines (25 loc) · 842 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
const express = require('express');
const axios = require('axios');
const app = express();
const port = 3000;
app.set('view engine', 'ejs');
app.use(express.static('public'));
app.use(express.urlencoded({ extended: true }));
app.get('/', (req, res) => {
res.render('index');
});
app.post('/get-joke', async (req, res) => {
const name = req.body.name;
try {
const response = await axios.get('https://v2.jokeapi.dev/joke/Any');
let joke = response.data.joke || `${response.data.setup} ... ${response.data.delivery}`;
joke = joke.replace(/Chuck Norris/g, name);
res.render('joke', { joke });
} catch (error) {
console.error(error);
res.render('error', { error: 'Error fetching joke. Please try again later.' });
}
});
app.listen(port, () => {
console.log(`App listening at http://localhost:${port}`);
});