-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
28 lines (22 loc) · 691 Bytes
/
server.js
File metadata and controls
28 lines (22 loc) · 691 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
28
const express = require('express');
const fetch = require('node-fetch');
const app = express();
const PORT = 3000;
const path = require('path');
app.use(express.static('public'));
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'views', 'index.html'));
});
app.get('/api/products', async (req, res) => {
try {
const response = await fetch('https://dummyjson.com/products');
const data = await response.json();
res.json(data);
} catch (error) {
console.error('Error fetching products:', error);
res.status(500).send('Error loading products.');
}
});
app.listen(PORT, () => {
console.log(`Server running at http://localhost:${PORT}`);
});