-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
37 lines (31 loc) · 1020 Bytes
/
Copy pathserver.js
File metadata and controls
37 lines (31 loc) · 1020 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
29
30
31
32
33
34
35
36
37
const express = require('express');
const axios = require('axios');
const cors = require('cors');
const bodyParser = require('body-parser');
const path = require('path');
const app = express();
const PORT = process.env.PORT || 3000;
// Middleware
app.use(cors());
app.use(bodyParser.json());
app.use(express.static(path.join(__dirname, 'public')));
// API proxy for weather data
app.get('/api/weather/:city', async (req, res) => {
const city = req.params.city;
try {
// Forward the request to our Java backend
const response = await axios.get(`http://localhost:8080/api/weather/${city}`);
res.json(response.data);
} catch (error) {
console.error('Error fetching weather data:', error);
res.status(500).json({ error: 'Failed to fetch weather data' });
}
});
// Serve the AngularJS application
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'index.html'));
});
// Start the server
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});