0

I want to have a lowdb Json database and in it I want to be able to get all the "urls" and list only the 'slug', the 'url', and the 'stats' for each and every single one like this (with JS,HTML)-> (Below is how it should be listed {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

And this is how the database would look:

{
  "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
    }
  ]
}

I am pretty new to all this stuff; how would you do it? Thanks in advance!

1 Answers1

0

I found the answer to my question (Thanks to @ch1ck3n) :

Since db.json looks like this:

{
  "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
    }
  ]
}

...Then index.js would look like this:

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');
});

A working demo is shown here.