In the previous course, we've created our own REST API. For that, we've created an Express project where we:
- Removed everything related to the views to sent JSON instead
- Added
cors
In term of routes, we have:
GET /api/phones
POST /api/phones
GET /api/phones/:id
PUT /api/phones/:id
DELETE /api/phones/:id
In our Express app, we will insert the React code in a folder /client
For that, generate the React app inside the Express app directory:
$ create-react-app client
$ npm install --save axios
We will create a client/src/api.js
// client/src/api.js
import axios from 'axios';
const service = axios.create({
baseURL: `http://localhost:3000/api/`
});
const errHandler = err => {
console.error(err.response.data);
throw err.response.data;
};
export default {
service: service,
getPhones() {
return service.get('phones')
.then(res => res.data)
.catch(errHandler);
},
getPhoneDetail(id) {
return service.get('phones/' + id)
.then(res => res.data)
.catch(errHandler);
},
addPhone(data) {
return service.post('phones', data)
.then(res => res.data)
.catch(errHandler);
},
modifyPhone(id, data) {
return service.post('phones/'+id, data)
.then(res => res.data)
.catch(errHandler);
},
deletePhone(id) {
return service.delete('phones/' + id)
.then(res => res.data)
.catch(errHandler);
},
};
Then, we can for example use getPhones()
like this:
import api from './api'
api.getPhones()
.then(data => {
console.log("This is my data:", data)
})
To test the application, you should have 2 terminals opened
# Terminal 1
$ npm start # or npm run dev
# Terminal 2
$ cd client
$ npm start
import React, { Component } from 'react'
import api from './api'
class ListPhones extends Component {
constructor(props) {
super(props)
this.state = {
phones: []
}
}
componentDidMount() {
api.getPhones()
.then(data => {
this.setState({
phones: data
})
})
}
render() {
return (
<div>
<ul>
{this.state.phones.map(phone => <li>{phone.name}</li>)}
</ul>
</div>
)
}
}
export default ListPhones
Create a component with an number input that displays the phone with the id precised in the input.
First, inside the client
folder, let's run the following command:
$ npm run build
It has just created a folder client/build
that contains some HTML, CSS and JS files readable by a browser.
Now that we have our client application bundled in the /client/build
folder we can move it into a static folder of our back-end app. We want our Express server to serve the entire client application.
For that, we should add the following line in app.js
app.use(express.static(path.join(__dirname, 'client/build')));
// ...
// The routes for the API
// ...
// This will be the default route is nothing else is caught
app.use(function(req, res) {
res.sendfile(__dirname + '/client/build/index.html');
});
In this lesson we have learned how to run a full-stack application, how to consume a REST API using Rest, and how to build an application for release.