Pulse7
9/6/2017 - 3:10 PM

Fetch http

<h1>Users</h1>
  <table>
    <thead>
      <th>&nbsp;</th>
      <th>Id</th>
      <th>First Name</th>
      <th>Last Name</th>
      <th>Email</th>
    </thead>
    <tbody id="users">

    </tbody>
  </table>
import {getUsers} from './api/userApi';

getUsers().then(result=>{
  let usersBody = "";
  console.log(result.forEach(user=>{
    usersBody+=`<tr>
    <td><a href="#" data-id="${user.id}" class="deleteUser">Delete</a></td>
    <td>${user.id}</td>
    <td>${user.firstName}</td>
    <td>${user.lastName}</td>
    <td>${user.email}</td>
    </tr>`
  }));
  global.document.getElementById('users').innerHTML = usersBody;
})
import 'whatwg-fetch'; //polyfill

export function getUsers(){
  return get('users');
}

function get(url){
  return fetch(url).then(onSuccess,onError);
}

function onSuccess(response){
  return response.json();
}

function onError(error){
  console.log(error); //eslint-disable-line no-console
}
app.get('/users', function (req, res) {
  res.json([
    {"id":1,"firstName":"Bob","lastName":"Smith","email":"bob@gmail.com"},
    {"id":2,"firstName":"Tammy","lastName":"Norton","email":"tnorton@yahoo.com"},
    {"id":3,"firstName":"Tina","lastName":"Lee","email":"lee.tina@hotmail.com"}
  ]);
});