55 lines
1.3 KiB
JavaScript
55 lines
1.3 KiB
JavaScript
import express from "express";
|
|
import cors from "cors";
|
|
import ky from "ky";
|
|
|
|
const app = express();
|
|
const PORT = 3000;
|
|
|
|
app.use(cors());
|
|
app.use(express.json());
|
|
|
|
app.get("/regions", async (req, res) => {
|
|
let regions = [];
|
|
|
|
const result = await ky
|
|
.get(
|
|
"https://xn--80az8a.xn--d1aqf.xn--p1ai/аналитика/квартирография/api/dictionaries/regions"
|
|
)
|
|
.json();
|
|
|
|
regions = result
|
|
.map((item) => item.regions)
|
|
.flat()
|
|
.sort((a, b) => (a.name > b.name ? 1 : -1));
|
|
|
|
res.send(regions);
|
|
});
|
|
|
|
app.get("/region/:id", async (req, res) => {
|
|
try {
|
|
const result = await ky
|
|
.get(
|
|
`https://xn--80az8a.xn--d1aqf.xn--p1ai/аналитика/квартирография/api/dashboard/by-room-count?regions=${req.params.id}`
|
|
)
|
|
.json();
|
|
|
|
const result2 = await ky
|
|
.get(
|
|
`https://xn--80az8a.xn--d1aqf.xn--p1ai/аналитика/api/rpp/?regionCode=${req.params.id}&repMonth=3&repYear=2023`
|
|
)
|
|
.json();
|
|
|
|
const { totalArea, flatCount } = result;
|
|
const averageApartmentArea = Math.round(totalArea / flatCount);
|
|
const { priceAvg } = result2.data.total[0];
|
|
|
|
res.send({ averageApartmentArea, priceAvg });
|
|
} catch (error) {
|
|
res.send({ error: 1 });
|
|
}
|
|
});
|
|
|
|
app.listen(PORT, () => {
|
|
console.log(`Server is started on port ${PORT}...`);
|
|
});
|