我的React前端和Node.js后端之间如何通信?

时间:2019-06-05 19:12:29

标签: reactjs

我想将我的ReactJS前端连接到node.js后端。前端和后端之间可用的通信方式有哪些?

我已经知道如何在组件中发出简单的fetch请求:

import React,{Component} from 'react';
import {Container,ListGroup,ListGroupItem,Button} from 'reactstrap';

class Courses extends Component{
    constructor(props){
        super(props);
        this.state ={
            courses :[],
            subjects:[]
        }
    }

    componentDidMount() {
        fetch('http://localhost:3000/api/courses/get-courses')
            .then(res => {
                return res.json();
            })
            .then(json  => {
                console.log(json);
                this.setState({
                    courses: json
                })
            })
    };

    render(){
        return(
            <Container>
                    <Button>Add Course</Button>
                <ListGroup>
                    {this.state.courses.map(courses => (
                        <ListGroupItem key={courses.id}>

                            <h2>{courses.name}</h2>
                            <h6>Code - {courses.code}</h6>
                            <h6>Pass Mark - {courses.passMark}</h6>
                            <h6>Lecturer in Charge - {courses.lecturerInCharge}</h6>
                            <h6>Subjects - {courses.subjects} </h6>


                        </ListGroupItem>
                    ))}
                </ListGroup>

            </Container>
        );
    }
}

export default Courses;

0 个答案:

没有答案