Created
December 28, 2018 14:25
-
-
Save talentlessguy/5a4fcf288f150d3ceb984a11176ead8f to your computer and use it in GitHub Desktop.
Axios & Express
This file contains 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 app = require('express')() | |
app.use((req, res, next) => { | |
res.header("Access-Control-Allow-Origin", "*"); | |
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept"); | |
res.header("Content-Type", "application/json") | |
next(); | |
}); | |
app.get('/', (req, res) => { | |
res.json({ | |
content: '<h1>Hello World</h1>' | |
}) | |
}) | |
app.listen(4000) |
This file contains 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 { hydrate } from 'react-dom' | |
import ReactHtmlParser from 'react-html-parser' | |
import axios from 'axios' | |
class App extends Component { | |
constructor() { | |
super() | |
this.state = { | |
content: '', | |
} | |
} | |
componentDidMount() { | |
axios.get('http://localhost:4000', { | |
headers: { | |
'Content-Type': 'application/json', | |
"Access-Control-Allow-Origin": "*" | |
} | |
}).then(res => this.setState({ content: res.data.content})) | |
} | |
render() { | |
return ( | |
<div> | |
{ReactHtmlParser(this.state.content)} | |
</div> | |
) | |
} | |
} | |
hydrate( | |
<App/>, document.getElementById('root') | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment