我正在使用gun db并测试gun模式。我正在使用以下代码,但未将数据保存在本地存储中

时间:2019-02-27 10:06:05

标签: javascript html gun gundb

详细说明::我提到了下面给出的两个文件的代码以供参考。我正在使用的枪的模式库未将数据保存在本地存储中。我是gun db的初学者,对此一无所知。有人可以帮我吗? 提到的细节     server.js ::::                 //导入本地http模块

            var http = require('http')

            // import gun

            var Gun = require('gun')

            var server = http.createServer(function(req, res){
              if (Gun.serve(req, res)){ return } // filters gun requests!
              require('fs').createReadStream(require('path').join(__dirname, req.url)).on('error',function(){ // static files!

            res.writeHead(200, {'Content-Type': 'text/html'});

             res.end(

             require('fs')
                  .readFileSync(require('path')
                  .join(__dirname, 'index.html') // or default to index
                ));
              }).pipe(res); // stream
            });


            // start listening for requests on `localhost:8080`
            server.listen(8080)
        ------------------------------------------------------- 
    index.html ::::
        <!-- index.html -->
        <html>
          <head>
            <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=0">
          </head>
          <body>
            <div id="App">
              <div id="Output"></div>
              <input
                type="text"
                id="Input"
                placeholder="Enter a message..."
                autofocus
              >
            </div>
            <script src="http://localhost:8080/gun.js"></script>
            <script>
              const gun = Gun('http://localhost:8080' + '/gun');

               const graphqlGun = require('graphql-gun');
               const Gun = require('gun');
               const gql = require('graphql-tag')

              // var jack  = gun.get('jack');
              // jack.put({name:'jack',occupation:'manager'})

              var fish = gun.get('fish');
              fish.put({red: {name: 'Frank'}});

              const $Input = document.querySelector('#Input');
              gun.get('test-input').val((data) => {
                $Input.value = data.value;
              })
              $Input.addEventListener('input', e => {
                const { value } = e.target;
                gun.get('test-input').put({
                  value
                });
              });

              let timestamp = 0;
              gun.get('test-input').on(function(data, key) {
                const ts = data._['>'].value;
                if (timestamp === ts) {
                  return;
                }
                timestamp = ts;
                console.log("update:", data);
                const $Output = document.querySelector('#Output');
                $Output.innerHTML = /* @html */`
                  <pre>
                    </code>
                      ${JSON.stringify(data, null, 2)}
                    </code>
                  </pre>
                `;
              });
            </script>
          </body>
        </html>

0 个答案:

没有答案