【发布时间】:2018-04-12 17:02:37
【问题描述】:
免责声明:我是 React 和 Nodejs 的新手,当然看过所有当前类似的帖子,但没有一个能够完全帮助我,所以请不要标记为重复。
在我的 Nodejs index.js 我有:
app.get('/list-certs', function (req, res) {
fs.readFile("certs.json", 'utf8', function (err, data) {
console.log( data );
res.send( data );
});
})
app.post('/list-certs', function (req, res) {
res.send('POST request: ' + req.body.domain-input);
console.log('post is working!')
});
在我的 React App.js 我有:
componentDidMount() {
this.callApi()
.then(res => {
this.setState({
json: res.map(x => x),
})
})
.catch(err => console.log(err));
}
callApi = async () => {
const response = await fetch('/list-certs');
const body = await response.json();
if (response.status !== 200) throw Error(body.message);
return body;
};
handleChange(event) {
this.setState({domainInputValue: event.target.value});
}
click() {
}
render() {
return (
<div className="App">
<form action="http://127.0.0.1:8000/list-certs" className="add-cert-form" method="post">
<h1>Add new domain</h1>
<h5 className="domain-label">Name:</h5>
<Input className="domain-input" value={this.state.domainInputValue} onChange={(e) => {this.handleChange(e)}}></Input>
<Button onClick={this.click} className="domain-button" type='primary'>Add</Button>
</form>
.
.
.
}
当单击按钮domain-button 时,如何将输入字段domain-input 中的数据发送到我的后端?我知道click() 需要输入一些内容,但我不确定是什么。
任何帮助表示赞赏!
【问题讨论】:
标签: node.js reactjs rest express post