Creating a user

The create method will take user data from the view component, use fetch to make a POST call to create a new user in the backend, and finally return the response from the server to the component as a promise.

mern-skeleton/client/user/api-user.js:

const create = (user) => {  return fetch('/api/users/', {      method: 'POST',      headers: {        'Accept': 'application/json',        'Content-Type': 'application/json'      },      body: JSON.stringify(user)    })    .then((response) => {      return response.json()    }).catch((err) => console.log(err))}

Get Full-Stack React Projects now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.