This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
app.put('/books/:id', (req, res) => { | |
const updateIndex = books.findIndex(book => book.id === req.params.id) | |
res.json(Object.assign(books[updateIndex], req.body)) | |
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
app.delete('/books/:id', (req, res) => { | |
const deletedIndex = books.findIndex(book => book.id === req.params.id) | |
books.splice(deletedIndex, 1) | |
res.status(204).send() | |
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const bodyParser = require('body-parser') | |
app.use(bodyParser.json()) | |
app.use(bodyParser.urlencoded({ extended: true })) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
app.post('/books', (req, res) => { | |
books.push(req.body) | |
res.status(201).json(req.body) | |
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
app.get('/books/:id', (req, res) => { | |
res.json(books.find(book => book.id === req.params.id)) | |
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const books = require('./db') | |
app.get('/books', (req, res) => { | |
res.json(books) | |
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const books = require('./db') | |
app.get('/books', (req, res) => { | |
res.json(books) | |
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[ | |
{ | |
"id": "1", | |
"name": "Game of thrones" | |
}, | |
{ | |
"id": "2", | |
"name": "Clash of kings" | |
} | |
] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const express = require('express') | |
const app = express() | |
app.get('/', (req, res) => { | |
res.send('Hello World') | |
}) | |
app.listen(3000, () => { | |
console.log('Start server at port 3000.') | |
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { Component } from 'react' | |
import Loading from '../components/Loading' | |
const withLoading = (WrappedComponent) => { | |
return class ComponentWithLoading extends Component { | |
render() { | |
const { isLoading } = this.props | |
if (isLoading) return <Loading /> | |
return <WrappedComponent {...this.props} /> |