如何将数据从服务器公开给客户端

时间:2021-09-24 02:35:10

I'm new with node.js. I'm writing a single rest API and I want to know what is the right way to expose data without expose unnecessary information.

我新的node . js。我正在编写一个rest API,我想知道如何在不暴露不必要的信息的情况下公开数据的正确方法。

In my example, I have a "Tag" schema. And I don't want to expose the mongoose fields to my client. My code:

在我的示例中,我有一个“标记”模式。我不想把mongoose字段暴露给我的客户。我的代码:

apiRoutes.get('/tag', passport.authenticate('jwt', {
    session: false
}), (req, res) => {
    Tag.find({}, (err, tags) => {
        return res.json(tags);
    });
});

But in client, I don't want to expose "_id" and "__v":

但是在客户端,我不想暴露“_id”和“__v”:

{
        "_id": "57083a5e725f3cf0242a2916",
        "tagName": "Test",
        "en_us": "Testing",
        "__v": 0,
        "lastUpdated": "2016-04-08T23:10:22.759Z"
    }

What is the right way to map only relevant fields?

只映射相关字段的正确方法是什么?

3 个解决方案

#1


2  

Your can always use mongoose virtuals. Here is an example:

你可以一直使用猫鼬的虚拟。这是一个例子:

In your model, you can use something like this:

在您的模型中,您可以使用如下内容:

Tag
  .virtual('public')
  .get(function() {
    return {
      tagName: this.tagName,
      en_us: this.en_us,
      lastUpdated: this.lastUpdated
    };
  });

Then, when making a query, just use the virtual you've created:

然后,在进行查询时,只需使用您创建的虚拟机:

Tag.find({}, (err, tags) => {
  res.json(tags.map(tag => tag.public));
});

#2


1  

You can use Schema transform toJSON. Here is a blog post explains it in details ignore certain fields from mongoose schema when return object to client

您可以使用模式转换到json。这是一篇博客文章,详细解释了在将对象返回给客户端时忽略mongoose模式中的某些字段

TagSchema.set('toJSON', {
  transform: function(doc, ret, options) {
    delete ret._id;
    delete ret.__v;
    return ret;
  }
});

#3


1  

You can declare required fields separated by space in find method:

可以在find方法中声明按空格分隔的必需字段:

Tag.find({}, 'tagName en-us', (err, tags) => {
    return res.json(tags);
});

Check mongoose documenation.

检查猫鼬documenation。

#1


2  

Your can always use mongoose virtuals. Here is an example:

你可以一直使用猫鼬的虚拟。这是一个例子:

In your model, you can use something like this:

在您的模型中,您可以使用如下内容:

Tag
  .virtual('public')
  .get(function() {
    return {
      tagName: this.tagName,
      en_us: this.en_us,
      lastUpdated: this.lastUpdated
    };
  });

Then, when making a query, just use the virtual you've created:

然后,在进行查询时,只需使用您创建的虚拟机:

Tag.find({}, (err, tags) => {
  res.json(tags.map(tag => tag.public));
});

#2


1  

You can use Schema transform toJSON. Here is a blog post explains it in details ignore certain fields from mongoose schema when return object to client

您可以使用模式转换到json。这是一篇博客文章,详细解释了在将对象返回给客户端时忽略mongoose模式中的某些字段

TagSchema.set('toJSON', {
  transform: function(doc, ret, options) {
    delete ret._id;
    delete ret.__v;
    return ret;
  }
});

#3


1  

You can declare required fields separated by space in find method:

可以在find方法中声明按空格分隔的必需字段:

Tag.find({}, 'tagName en-us', (err, tags) => {
    return res.json(tags);
});

Check mongoose documenation.

检查猫鼬documenation。