axios 我想使用express js在调用端点上返回特定响应

cnh2zyt3  于 2023-02-04  发布在  iOS
关注(0)|答案(1)|浏览(116)

this is my code that i use to get air quality in the nearest city
this is the response that i get
i want to return a part of this response and it will be like this
我希望得到一个特别答复:我只想退回这部分:“污染”:{“ts”:“2023-02-03T06:00:00.000Z”,“aquius”:29、“美纽斯”:“p2”、“aqicn”:10、“维护”:“p2”}

0qx6xfy6

0qx6xfy61#

这个代码会起作用。

res.send({ result : { pollution : response.data.data.current.pollution } })

这是模拟rest API调用的演示代码

const express = require("express")
const axios = require('axios')
const cors = require("cors")

const app = express()
app.use(cors())

const API_URL = 'http://localhost:3001/users'

app.get("/whole-data", async (req, res) => {
    const response = await axios.get(`${API_URL}`)
    return res.send(response.data)
});

app.get("/air-quality", async (req, res) => {
    const response = await axios.get(`${API_URL}`)
    return res.send({ result : { pollution : response.data.data.current.pollution } })
});

app.listen(3000, () => { console.log("Listening on :3000") })

仅污染

全部数据

相关问题