repl.it
@timmy_i_chen/

jsonstore.io example

Nodejs

a simple example of using jsonstore.io in a node app

fork
loading
Files
  • index.js
  • db.js
  • env
  • Packager files
  • package-lock.json
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
const express = require('express');
const { get, post } = require('./db');

const app = express();

let list = [];

app.get('/', (req, res) => {
  get()
    .then(({ data }) => {
      console.log(data.result && data.result.body)
      list = data.result && data.result.body || [];
      const listAsString = list.length ? list.join(', ') : 'There\'s nothing in the list!';
      res.send(listAsString + '<br/><br/>go to /add/something to add to the list, go to /clear to clear list');
    });
});

app.get('/add/:thing', (req, res) => {
  list.push(req.params.thing);
  post(list).then(() => res.redirect('/'));
});

app.get('/clear', (req, res) => {
  post([]).then(() => res.redirect('/'));
})

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