简体   繁体   English

Express,从 pons API 获取数据

[英]Express, fetching data from pons API

I am new to express and I try to fetch data from Pons API: https://en.pons.com/p/files/uploads/pons/api/api-documentation.pdf我是新手,我尝试从 Pons API 获取数据: https ://en.pons.com/p/files/uploads/pons/api/api-documentation.pdf

here is my file:这是我的文件:

const express = require("express");
const fetch = require("node-fetch");

const app = express();

app.get("/", async (req, res, next) => {
 const answer = await fetch(
   "https://api.pons.com/v1/dictionary?q=dom&l=depl",
   {
     method: "get",
     headers: {
       "X-Secret":
         "XXX",
     },
   }
 )
   .then((res) => res.json())
   .then((json) => console.log(json))
   .catch((err) => console.log(err));
});

app.listen(5000);

In my console I only see this: [ { lang: 'pl', hits: [ [Object], [Object] ] }, { lang: 'de', hits: [ [Object] ] } ] I would like to get translations in german for the word "dom".在我的控制台中,我只看到: [ { lang: 'pl', hits: [ [Object], [Object] ] }, { lang: 'de', hits: [ [Object] ] } ] 我想得到“dom”这个词的德语翻译。 Can somebody please tell me what do I have wrong?有人可以告诉我我有什么问题吗?

You are not doing anything wrong.你没有做错任何事。 You just need some additional code to access the Objects in the response, which is a regular JavaScript object, but console.log() is just not printing it all.您只需要一些额外的代码来访问响应中的对象,这是一个常规的 JavaScript 对象,但console.log()并没有全部打印出来。 The translation you are looking for is contained somewhere in there.您正在查找的翻译包含在其中的某处。 Just be aware that there can be multiple responses as the same word can have different meanings or can be of different word classes (noun, verb, adjective, etc.)请注意,可能有多个响应,因为同一个词可能有不同的含义,也可能属于不同的词类(名词、动词、形容词等)

What you can do to get a better understanding of the objects is to use JSON.stringify() to print the whole structure.为了更好地理解对象,您可以使用JSON.stringify()来打印整个结构。 For example:例如:

.then((json) => console.log(JSON.stringify(json, null, 2)))

Note: The third paramter 2 means that an indentation of 2 spaces will be used.注意:第三个参数2表示将使用 2 个空格的缩进。

My approach to the Pons Api on the server我在服务器上使用 Pons Api 的方法

const utils = require('./lib/utils')

app.get("/dic/:word", (req, res, next) => {
utils.getLastLine('api_key', 1).then(key => {
key = key.split('=')[1]
var word = req.params.word
var translate = 'dees'
return fetch(
 `https://api.pons.com/v1/dictionary?q=${word}&l=${translate}`,{
  headers: { "X-Secret": key }}
)
    .then(res => res.json()).then(json => {
    var base = json[0].hits[0].roms[0]
    var wclass = base.wordclass
    var arab = base.arabs[0]
    var trans = arab.translations
    var firsttrans = trans[0].target
    var nrtrans = arab.translations.length
    res.json({ firsttrans, wclass, nrtrans, trans })
   })
 });
});

Requirements:要求:

Further/Limits:进一步/限制:

  • It is a nested fetch这是一个嵌套fetch
  • See a live example看一个活生生的例子
  • Code example is for development, not for production:代码示例用于开发,而不是用于生产:
    • no server error catching in case of在以下情况下不会捕获服务器错误
      • no input无输入
      • non-existing word不存在的词
    • not all Api results are considered, just the first hit并未考虑所有 Api 结果,仅考虑第一次hit

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM