首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Node.js中的HATEOAS

Node.js中的HATEOAS
EN

Stack Overflow用户
提问于 2022-03-07 07:18:45
回答 1查看 220关注 0票数 0

我想使用快速仇恨链接包,但它不起作用:

"C:/Users/me/Documents/node/express-project/node_modules/express-hateoas-links/index“模块

找不到模块‘快递-仇恨-链接’的声明文件。'C:/Users/me/Documents/node/express-project/node_modules/express-hateoas-links/index.js‘隐式地具有“任意”类型。如果npm i --save-dev @types/express-hateoas-links存在,可以尝试使用它,或者添加一个包含declare module 'express-hateoas-links';ts的新声明(.d.ts)文件(7016)

代码语言:javascript
复制
import express, { json } from 'express';
import hateoasLinker from 'express-hateoas-links';

const friends = [
  {
    id: 0,
    name: 'XXX'
  },
  ...
];

app.use(hateoasLinker);

app.use((req, res, next) => {
  console.log(`${req.method} ${req.url}`);
  next();
});

app.use(json());

app.get('/friends', (req, res) => {
  res.json(friends,[
    { rel: "self", method: "GET", href: 'http://localhost:3000/friends' },
    { rel: "create", method: "POST", title: 'Create Friends', href: 'http://localhost:3000/friends' }
  ]);
});

应用程序可以工作,但由于错误,响应不包含链接。???

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-10-13 08:37:47

我记得当我尝试使用HATEAOS库不能用于数组JSON响应时,但我尝试使用map方法

代码语言:javascript
复制
const response = catalogo.map(x => {
  return {
    name: x.name,
    description: x.description,
    href: {
      rel: "productos",
      method: "GET",
      href: 'http://localhost:8080/ver-productos/' + x._id
    },
  }
});

res.json({
  from,
  limit,
  total,
  response
});

这对我有用。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71377594

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档