从lowdb json 数据库中列出一些带有JS、HTML 的值

问题描述

我想要一个 lowdb Json 数据库,在其中我希望能够获取所有“url”并仅列出每个“slug”、“url”和“stats”像这样(使用JS,HTML)-> (下面是它应该如何被列出 {in HTML,JS})

Slug: Example
Url: https://example.com
Stats: 37

Slug: Example1234
Url: https://example.net
Stats: 15

Slug: Exampleio
Url: https://example.io
Stats: 20

这就是数据库的样子:

{
  "urls": [
    {
      "slug": "example","url": "https://example.com","token": "amazingdev1","stats": 37
    },{
      "slug": "example1234","url": "https://example.net","token": "adsfrdmgsrf","stats": 15
    },{
      "slug": "exampleio","url": "https://example.io","token": "07dfpwxukck57rv5","stats": 20
    }
  ]
}

我对所有这些东西都很陌生;你会怎么做? 提前致谢!

解决方法

我找到了问题的答案(感谢@ch1ck3n):

因为 db.json 看起来像这样:

{
  "urls": [
    {
      "slug": "example","url": "https://example.com","token": "ah4ret7w","stats": 37
    },{
      "slug": "example1234","url": "https://example.net","token": "adsfrdmgsrf","stats": 15
    },{
      "slug": "exampleio","url": "https://example.io","token": "07dfpwxukck57rv5","stats": 20
    }
  ]
}

...那么 index.js 将如下所示:

const express = require('express');

const app = express();

const low = require('lowdb')
const FileSync = require('lowdb/adapters/FileSync')

const adapter = new FileSync('db.json')
const db = low(adapter)

re = ""
db.get('urls').__wrapped__.urls.forEach(function(item){
  re += "<div class='card' style='border: 5px solid black'>"
  re += ("Slug: " + item.slug +"<br>");
  re += ("Url: " + item.url+"<br>")
  re += ("Stats: " + item.stats+"<br>")
  re += "</div>"
  re += ("<br>")
});

app.get('/',(req,res) => {
  res.send(re)
});

app.listen(3000,() => {
  console.log('server started');
});

显示了一个工作演示here