concept json - server in category react

appears as: json-server, json-server
Redux in Action

This is an excerpt from Manning's book Redux in Action.

  • Install and configure json-server, a popular tool for generating REST APIs quickly. This is a Redux book after all, so you don’t want to expend too much energy writing a fully featured back end. json-server allows you to specify a set of resources (such as tasks) and creates a standard set of endpoints to use.
  • Listing 4.4. src/api/index.js
    import axios from 'axios'; const API_BASE_URL = 'http://localhost:3001'; #1 const client = axios.create({ baseURL: API_BASE_URL, headers: { 'Content-Type': 'application/json', #2 }, }); export function fetchTasks() { #3 return client.get('/tasks'); #3 } #3 #1 Defines a constant for the API’s base URL #2 The Content-Type header is required by json-server for PUT requests. #3 Exports a named fetchTasks function that will make the call !@%STYLE%@! {"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":2,\"ch\":51},{\"line\":2,\"ch\":53}],[{\"line\":15,\"ch\":0},{\"line\":15,\"ch\":2}],[{\"line\":7,\"ch\":51},{\"line\":7,\"ch\":53}],[{\"line\":16,\"ch\":0},{\"line\":16,\"ch\":2}],[{\"line\":11,\"ch\":51},{\"line\":11,\"ch\":53}],[{\"line\":12,\"ch\":51},{\"line\":12,\"ch\":53}],[{\"line\":13,\"ch\":51},{\"line\":13,\"ch\":53}],[{\"line\":17,\"ch\":0},{\"line\":17,\"ch\":2}],[{\"line\":11,\"ch\":51},{\"line\":11,\"ch\":53}],[{\"line\":12,\"ch\":51},{\"line\":12,\"ch\":53}],[{\"line\":13,\"ch\":51},{\"line\":13,\"ch\":53}],[{\"line\":17,\"ch\":0},{\"line\":17,\"ch\":2}],[{\"line\":11,\"ch\":51},{\"line\":11,\"ch\":53}],[{\"line\":12,\"ch\":51},{\"line\":12,\"ch\":53}],[{\"line\":13,\"ch\":51},{\"line\":13,\"ch\":53}],[{\"line\":17,\"ch\":0},{\"line\":17,\"ch\":2}]]"} !@%STYLE%@!