我想使用快速仇恨链接包,但它不起作用:
"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)
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' }
]);
});应用程序可以工作,但由于错误,响应不包含链接。???
发布于 2022-10-13 08:37:47
我记得当我尝试使用HATEAOS库不能用于数组JSON响应时,但我尝试使用map方法
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
});这对我有用。
https://stackoverflow.com/questions/71377594
复制相似问题