向节点服务器发布请求未命中端点

时间:2018-11-04 15:48:31

标签: javascript node.js reactjs

我正在尝试将axios.post从我的反应前端发送到我的节点服务器! react客户端和节点服务器都在本地运行-在同一父目录中。客户端在端口3000和服务器3400上运行。

当我将代码更改为app.get并访问url localhost:3400 / refundCalc时,它工作并在重新发送时显示问候消息。它不能作为app.post使用,但也不能通过axios发布使用!

错误代码是:无法加载http://localhost:3400/refundCalc:对预检请求的响应未通过访问控制检查:请求的资源上不存在“ Access-Control-Allow-Origin”标头。因此,不允许访问来源“ http://localhost:3000”。

我发布了所有的React前端代码,但实际上,它只是getRefundCalc函数,那是axios.post的问题,我想吗?

我的服务器代码:

const express = require('express');
const app = express();
const bodyParser = require('body-parser')
const port = 3400;
const server = app.listen(port, () => console.log(`Example app listening on port ${port}!`))
app.use(bodyParser.urlencoded({ extended: false }))

app.get('/', (req, res) => res.send('Hello World!'))


// to send the get refundCalcRequest
app.post('/refundCalc', function (req, res) {
    console.log('response from client - get refund request ' + res)
    console.log('request from client - get refund request ' + req)

    res.end('HELLOOOO')

})

我的反应前端:

import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { withStyles } from '@material-ui/core/styles';
import Card from '@material-ui/core/Card';
import CardActionArea from '@material-ui/core/CardActionArea';
import CardActions from '@material-ui/core/CardActions';
import CardContent from '@material-ui/core/CardContent';
import CardMedia from '@material-ui/core/CardMedia';
import Button from '@material-ui/core/Button';
import Typography from '@material-ui/core/Typography';
import axios from 'axios'

  class MediaCard extends Component {
  constructor(props) {

    super(props)
    this.state = {

    }

  }


  getRefundCalc(props) {
    console.log('we are calling the getrefundcalc call on button click')
    console.log('the props of the card, are they different for each card?' + JSON.stringify(props))

    axios.post(`http://localhost:3400/refundCalc`, { props }) <-- HERES THE CALL.
      .then(res => {
        console.log(res);
        console.log(res.data);
      })
  }




 render() {
    const { classes } = this.props;
    const rtnBtnClicked = this.state.rtnBtnClicked;
    return (
      <Card className={classes.card} onClick={() => this.getRefundCalc(this.props)}>
        <CardActionArea >
          <CardMedia
            className={classes.media}
            image={this.props.data.imageLinks.image_src}
          />
          <CardContent >
            <Typography gutterBottom variant="h5" component="h5" style={{ fontSize: 9 }}>
              {this.props.data.items.title}
            </Typography>
            <Typography component="h5" style={{ fontSize: 10 }}>
              Price:£ {this.props.data.items.price}
            </Typography>
          </CardContent>
        </CardActionArea>
        <CardActions>
          <Button size="small" color="primary" >
            {rtnBtnClicked ? 'Remove From Refund' : 'Add to Refund'}
          </Button>
        </CardActions>
      </Card>
    );
  }
}

我的预期结果是帖子请求命中我的端点并控制结果:D

1 个答案:

答案 0 :(得分:1)

const express = require('express');
const app = express();
const bodyParser = require('body-parser')
const corst = require('cors'); // requiring cors
const port = 3400;

app.use(cors()) // adding cors middleware to allow request from other domains
app.use(bodyParser.urlencoded({ extended: false }))

...
const server = app.listen(port, () => console.log(`Example app listening on port ${port}!`))