Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
1.3k views
Welcome To Ask or Share your Answers For Others

1 Answer

First frontend = browser, backend = nodejs. You surely mean that LowDB is available as a NodeJS module, which means it can be installed with NPM, either for frontend or backend development.

If you're not using NPM for your frontend development, you can still import modules with <script> tags.

Let's see an example using only HTML/javascript:

(from the doc):

<html>
    <head>
        <script src="https://unpkg.com/lodash@4/lodash.min.js"></script>
        <script src="https://unpkg.com/lowdb@0.17/dist/low.min.js"></script>
        <script src="https://unpkg.com/lowdb@0.17/dist/LocalStorage.min.js"></script>
    </head>
    <body>

        <button onclick="add()">Add Post</button>
        <button onclick="load()">Load Post</button>
        <div></div>
    </body>
    <script>
        var adapter = new LocalStorage('db')
        var db = low(adapter)
        db.defaults({ posts: [] })
            .write()
        function add() {
          // Data is automatically saved to localStorage
          db.get('posts')
            .push({ title: 'lowdb' })
            .write()
        }
        function load() {
          var div = document.querySelector('div')
          var text = div.innerText;
          text += db.get('posts').map(post=>post.title).join(';');
          div.innerText = text 
        }
    </script>

</html>

See this work: https://codepen.io/bcaure/pen/ExgBPBz


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...