96 lines
2.2 KiB
JavaScript
96 lines
2.2 KiB
JavaScript
const express = require("express");
|
|
const cors = require("cors");
|
|
const mongoose = require("mongoose");
|
|
const mongoDB = "mongodb://127.0.0.1/chillroom-database";
|
|
const dbSchema = require("./dbScheme");
|
|
mongoose.connect(
|
|
mongoDB,
|
|
{ useNewUrlParser: true }
|
|
);
|
|
const app = express();
|
|
app.use(cors());
|
|
app.options("*", cors());
|
|
const port = 3000;
|
|
|
|
let interval = setInterval(() => {
|
|
(async url => {
|
|
const value_data = await getScript(url);
|
|
console.log(value_data);
|
|
// const item = new dbSchema({
|
|
// name: value_data.split(",")[0],
|
|
// value: value_data.split(",")[1]
|
|
// });
|
|
// await item.save();
|
|
})("http://localhost:3000/chillroom-server?ip=192.168.8.107");
|
|
}, 5000);
|
|
|
|
const getScript = url => {
|
|
return new Promise((resolve, reject) => {
|
|
const http = require("http");
|
|
|
|
let client = http;
|
|
|
|
url = "http://" + url + "/json";
|
|
|
|
client
|
|
.get(url, resp => {
|
|
let data = "";
|
|
|
|
// Dane zostały pobrane
|
|
resp.on("data", chunk => {
|
|
data += chunk;
|
|
});
|
|
|
|
// Cały response pobrany i print
|
|
resp.on("end", () => {
|
|
resolve(data);
|
|
console.log(data);
|
|
});
|
|
})
|
|
.on("error", err => {
|
|
reject(err);
|
|
});
|
|
});
|
|
};
|
|
|
|
app.get("/chillroom-server", (req, res) => {
|
|
(async url => {
|
|
res.json(await getScript(url));
|
|
})(req.query.ip);
|
|
});
|
|
|
|
app.get("/stop", (req, res) => {
|
|
clearInterval(interval);
|
|
interval = null;
|
|
res.json({ message: "Przestalem dzialac :<" });
|
|
});
|
|
|
|
app.get("/start", (req, res) => {
|
|
if (interval === null) {
|
|
interval = setInterval(() => {
|
|
(async url => {
|
|
const value_data = await getScript(url);
|
|
console.log(value);
|
|
const item = new dbSchema({
|
|
name: value_data.split(",")[0],
|
|
value: value_data.split(",")[1]
|
|
});
|
|
await item.save();
|
|
})("http://localhost:3000/chillroom-server?ip=192.168.8.107");
|
|
}, 5000);
|
|
|
|
res.json({ message: "Zaczalem dzialac :>" });
|
|
} else {
|
|
res.json({ message: "Dzialam i nie mozna mnie rozpoczac raz jeszcze :p" });
|
|
}
|
|
});
|
|
|
|
app.get("/all", async (req, res) => {
|
|
const items = await dbSchema.find({});
|
|
res.json(items);
|
|
});
|
|
|
|
app.listen(port, () =>
|
|
console.log(`Chillroom server nasluchuje na porcie ${port}!`)
|
|
);
|