-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
74 lines (60 loc) · 1.99 KB
/
Copy pathindex.js
File metadata and controls
74 lines (60 loc) · 1.99 KB
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
64
65
66
67
68
69
70
71
72
73
74
const express = require('express');
const app = express();
const exphbs = require('express-handlebars');
const axios = require('axios');
const PORT = process.env.PORT || 3000;
const path = require('path');
// Set up Handlebars as the template engine
app.engine('hbs', exphbs.engine({
defaultLayout: 'main',
extname: '.hbs',
helpers: {
eq: (a, b) => a === b, // Helper for equality check
},
}));
app.set('view engine', 'hbs');
app.set('views', path.join(__dirname, 'views'));
// Serve static files
app.use(express.static(path.join(__dirname, 'public')));
// Creating Random route
const fortunes = ['fortune1', 'fortune2', 'fortune3', 'fortune4', 'fortune5', 'fortune6', 'fortune7', 'fortune8', 'fortune9', 'fortune10'];
// mapping names to countries
const countryToName = {
'Canada': 'Emi',
'Sri Lanka': 'Thinal',
'India': 'Varun',
'Indonesia': 'Riska',
'Poland': 'Polaczku',
'United Arab Emirates': 'Mateusz'
};
const cityToName = {
'Poznan' : 'Penero Okropna Poznańska',
'Gdansk' : 'Gruzin',
'Warszawa' : 'Elegancki Stołeczniaku'
}
// Routing
app.get('/', async (req, res) => {
const ip = req.headers['x-forwarded-for'] || req.ip;
try {
const response = await axios.get(`http://ip-api.com/json/${ip}`);
const country = response.data.country;
const city = response.data.city;
const name = cityToName[city] || countryToName[country] || 'Chris';
res.render('home', { name, country });
console.log(country, name, city);
} catch (err) {
console.error('Error Fetching Location:', err);
res.render('home', {name: 'Chris', country: null});
}
});
app.get('/random', (req, res) => {
// Pick a random route from the array
const randomFortune = fortunes[Math.floor(Math.random() * fortunes.length)];
res.render(randomFortune);
});
app.use((req, res) => {
res.render('404')
});
app.listen(PORT, function () {
console.log(`Server started on port: ${PORT}`);
});