为什么我的React应用程序在后端端口上启动?

时间:2019-01-22 07:58:06

标签: node.js reactjs

我有一个React和NodeJS应用程序。这是我的server / index.js文件:

@Entity
public class Emp {

    @Id 
    int empId;
    String empName;
    @ManyToMany(mappedBy="emps")
    List<Dept> depts;

    public Emp() {
        // TODO Auto-generated constructor stub
    }

    /**
     * @param empId
     * @param empName
     * @param depts
     */
    public Emp(int empId, String empName) {
        super();
        this.empId = empId;
        this.empName = empName;
    }
    public int getEmpId() {
        return empId;
    }
    public void setEmpId(int empId) {
        this.empId = empId;
    }
    public String getEmpName() {
        return empName;
    }
    public void setEmpName(String empName) {
        this.empName = empName;
    }
    public List<Dept> getDepts() {
        return depts;
    }
    public void setDepts(List<Dept> depts) {
        this.depts = depts;
    }

    @Override
    public String toString() {
        return "Emp [empId=" + empId + ", empName=" + empName + ", depts=" + depts + "]";
    }

}


Dept.java
@Entity
public class Dept {

    @Id 
    int deptId;
    String deptName;
    @ManyToMany()
    List<Emp> emps;

    public Dept() {
        // TODO Auto-generated constructor stub
    }

    /**
     * @param deptId
     * @param deptName
     * @param emps
     */
    public Dept(int deptId, String deptName) {
        super();
        this.deptId = deptId;
        this.deptName = deptName;
    }



    public int getDeptId() {
        return deptId;
    }
    public void setDeptId(int deptId) {
        this.deptId = deptId;
    }
    public String getDeptName() {
        return deptName;
    }
    public void setDeptName(String deptName) {
        this.deptName = deptName;
    }
    public List<Emp> getEmps() {
        return emps;
    }
    public void setEmps(List<Emp> emps) {
        this.emps = emps;
    }
    @Override
    public String toString() {
        return "Dept [deptId=" + deptId + ", deptName=" + deptName + ", emps=" + emps + "]";
    }   

}

App.java
public class App {

    public static void main(String[] args) {

        Emp emp1 = new Emp(1, "X");
        Emp emp2 = new Emp(2, "Y");
        Emp emp3 = new Emp(3, "Z");
        List<Emp> empList = new ArrayList<>(Arrays.asList(emp1, emp2, emp3));

        Dept dept1 = new Dept(1, "Physics");
        Dept dept2 = new Dept(2, "Math");
        Dept dept3 = new Dept(3, "Chemistry");
        List<Dept> deptList = new ArrayList<>(Arrays.asList(dept1, dept2, dept3));

        emp1.setDepts(deptList);
        dept2.setEmps(empList);

        SessionFactory factory = new Configuration().configure("hibernate.cfg.xml").addAnnotatedClass(Emp.class)
                .addAnnotatedClass(Dept.class).buildSessionFactory();

        Session session = factory.openSession();
        session.beginTransaction();

        //Write business logic
        session.save(emp1);
        session.save(emp2);
        session.save(emp3);

        session.save(dept1);
        session.save(dept2);
        session.save(dept3);

        Emp e = session.get(Emp.class, 1);
        List<Dept> depts = e.getDepts();

        for(Dept d : depts)
            System.out.println(e.getEmpId()+" .. "+d.getDeptId()+" .. "+d.getDeptName());


        session.getTransaction().commit();
        session.close();

当我执行npm start时,它试图在后端服务器上启动React服务器,该服务器的默认值为3333,默认值为3000。有人知道原因吗?

1 个答案:

答案 0 :(得分:0)

此问题可能有两个原因:

在这种情况下,react-scriptsfind next available free port(可能是3333?)并在那里启动服务器。

  • 您可能已经导出了PORT环境变量。

如果PORT环境变量可用react-scripts uses it to start the server