-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
34 lines (28 loc) · 893 Bytes
/
app.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
30
31
32
33
34
const express = require("express");
const http = require("http");
const socketIo = require("socket.io");
const axios = require("axios");
const port = process.env.PORT || 4001;
const index = require("./routes/index");
const app = express();
app.use(index);
const server = http.createServer(app);
const io = socketIo(server);
io.on("connection", socket => {
console.log("New client connected"), setInterval(
() => getApiAndEmit(socket),
10000
);
socket.on("disconnect", () => console.log("Client disconnected"));
});
const getApiAndEmit = async socket => {
try {
const res = await axios.get(
'http://api.openweathermap.org/data/2.5/weather?zip=60605,us&appid={apikey}'
);
socket.emit("FromAPI", res.data.main.temp);
} catch (error) {
console.error(`Error: ${error.code}`);
}
};
server.listen(port, () => console.log(`Listening on port ${port}`));