我在检索Aeries API数据并将其发送回EJS文件时遇到问题

时间:2019-03-10 16:11:26

标签: node.js ajax api request request-promise

我正在使用Node和request-promise。我想做的是让用户输入学校代码,然后呈现aeries.ejs页面以及他们输入的学校代码的API数据。我不知道该如何做一辈子。如何获取和发送数据?我不得不剪掉一些代码,因为它说大部分是代码。任何帮助将不胜感激,谢谢!

EJS:

<form action='/aeries/school/' method="GET">
    <input type='text' name = "code">
    <button>Submit</button>
</form>

JS:

const uriBase = 'https://demo.aeries.net/aeries/api/v3/';

var options = {
    method: "GET",
    uri: 'https://demo.aeries.net/aeries/api/v3/',
    headers: {
        'Accept': 'application/json, text/html, application/xhtml+xml, */*',
        'AERIES-CERT' : '477abe9e7d27439681d62f4e0de1f5e1'
    },
    json: true // Automatically parses the JSON string in the response
};
app.get('/aeries/school/:code', async (req,res)=>{
    const { code } = req.params;
    if(!code) return res.end('No code provided');
    options.uri = `${uriBase}/schools/${code}`;
    let data;
    try {
        data = await rp(options);
        res.send(data)
    } catch(err) {
        res.send({err, msg: 'No school found'});

})

0 个答案:

没有答案