imnileshd / react-todo-app

Build a CRUD app in React
7 stars 0 forks source link

Could you provide the rest api spec ? #1

Open jrichardsz opened 5 months ago

jrichardsz commented 5 months ago

Could you provide the rest api spec?

I mean only the expected request and response

Thanks

jrichardsz commented 5 months ago

I achieved to recreate the api

libraries

npm install express body-parser cors

server.js

var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var port = process.env.PORT || 5000;
var cors = require('cors');

var tasks = [{_id: "f98decd1-0899-444b-a33f-0efc611e1b4d", title: "foo", completed: true}];

app.use(bodyParser.urlencoded({extended: false}));
app.use(bodyParser.json());
app.use(cors());

app.get('/api/v1/tasks', function(req, res) {
  return res.json({"tasks": tasks});
});

app.post('/api/v1/tasks/', function(req, res) {
    tasks.push({...req.body, _id: uuidv4()})
    return res.json({"code": 200000, message: "success"});
});

app.patch('/api/v1/tasks/:id', function(req, res) {
    var foundIndex = tasks.findIndex(x => x._id == req.params.id);
    tasks[foundIndex] = req.body;
    return res.json({"code": 200000, message: "success"});
});

app.delete('/api/v1/tasks/:id', function(req, res) {
  var foundIndex = tasks.findIndex(x => x._id == req.params.id);
  if(foundIndex<0) {
    return res.json({"code": 404000, message: "No todo found"});    
  }

  tasks.splice(foundIndex, 1)
  return res.json({"code": 200000, message: "success"});
});

function uuidv4() {
  return "10000000-1000-4000-8000-100000000000".replace(/[018]/g, c =>
    (+c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> +c / 4).toString(16)
  );
}

app.listen(port);

console.log(`api mock is ready at ${port}`)