-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
27 lines (21 loc) · 725 Bytes
/
Copy pathserver.js
File metadata and controls
27 lines (21 loc) · 725 Bytes
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
const express = require('express');
const cors = require('cors');
const path = require('path');
const lebronFact = require('./data');
const app = express();
const PORT = process.env.PORT || 3000;
app.use(cors());
app.use(express.json());
app.use(express.static(path.join(__dirname)));
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'index.html'));
});
app.get('/api/fact', (req, res) => {
const randomIndex = Math.floor(Math.random() * lebronFact.length);
const fact = lebronFact[randomIndex];
res.json(fact);
});
app.listen(PORT, () => {
console.log(`✅ Lebron James Facts API running on port: ${PORT}`);
console.log(`🎲 Random fact: http://localhost:${PORT}/api/fact`);
});