Friday, 28 April 2017

How can I create session scoped requests with json-server in heroku?

I was after a free online tool which will allow me to actual invoke CRUD operations with JSON. ( not only invoke , but actually keep the modified data) - So now I'm able to use it in Plnkr/Jsbin/Jsfiddle/Codepen.

So I've used heroku and json-server and created an app which allows me to create persistent results for CRUD operation.

This is the app : https://server71.herokuapp.com/

//app.js

const { exec } = require('child_process');

// Set port (default: 3000). For Heroku, we need to use
// the port set by the environment variable $PORT
const port = process.env.PORT || 3000;

const command = `json-server --watch db.json --port ${port}`;

exec(command, (err, stdout, stderr) => {
  if (err) {
    console.log('Error running exec', err);
    return;
  }
  console.log('stdout:', stdout);
  console.log('stderr:', stderr);
});

So where is the problem ?

The problem is that the data is shared. So If I created a specific data structure and someone use /delete/1 , the it will actually delete the data for all users who uses this app.

Question:

How can I use json-server / heroku so that it will be per session ( and not globally).

I was thinking about something like :

1) Create session Guid
2) prepend this session guid to all request : something like :

https://server71.herokuapp.com/{c82cf96c-5d65-4dd8-85dc-2d3e87a37b89}/posts 

How can I make requests to be scoped to aguid ? ( or maybe other solution?)



via Royi

No comments:

Post a Comment