1
0
Fork 0
mirror of https://github.com/terribleplan/next.js.git synced 2024-01-19 02:48:18 +00:00
next.js/examples/with-custom-reverse-proxy/pages/index.js
Tran Dac Anh c87b471b22 Example to add custom reverse proxy like in webpack-dev-server (#2660)
* add with-custom-reverse-proxy example

* cleanup

* cleanup package.json

* fix linting errors

* more linting errors
2017-07-29 14:45:12 +02:00

46 lines
974 B
JavaScript

import React from 'react'
export default class extends React.Component {
constructor (props) {
super(props)
this.state = { response: '' }
}
static async getInitialProps ({ pathname, query }) {
return {
pathname,
query,
queryString: Object.keys(query).join('')
}
}
async componentDidMount () {
const response = JSON.stringify(
await window
.fetch(`/api/${this.props.queryString}`)
.then(response => response.json().then(data => data)),
null,
2
)
this.setState({ response })
}
render () {
return (
<content>
<p>
/api/{this.props.queryString} routed to https://swapi.co/api/{this.props.queryString}
</p>
<p>
<a href='?people/2'>Try</a>
&nbsp;
<a href='/'>Reset</a>
</p>
<pre>
{this.state.response ? this.state.response : 'Loading...'}
</pre>
</content>
)
}
}