【问题标题】:How do I run my app on port 3000? (React)如何在端口 3000 上运行我的应用程序? (反应)
【发布时间】:2020-10-27 20:48:13
【问题描述】:

我有一个后端在 localhost:8000 上运行的反应应用程序。前端部分应该在端口 3000 上运行。对于这个特定的示例,我使用的是 express 服务器。我该如何配置?

这是我的 server.js:

const express = require('express');
const cors = require('cors');
const path = require('path');
const testimonialsRoutes = require('./routes/testimonials.routes');
const concertsRoutes = require('./routes/concerts.routes');
const seatsRoutes = require('./routes/seats.routes');

const app = express();


app.use(express.urlencoded({ extended: false }));
app.use(express.json());
app.use(express.static(path.join(__dirname + '/client')));
app.use(cors());
app.use('/api/', testimonialsRoutes);
app.use('/api/', concertsRoutes);
app.use('/api/', seatsRoutes);

app.get('*', (req, res) => {
  res.sendFile(path.join(__dirname + '/client/NewWaveFest/public/index.html'));
});

app.use((req, res) => {
  res.status(404).json({ message: 'Not found' });
});

app.listen(process.env.PORT || 8000, () => {
  console.log('Server is running on port: 8000');
});

【问题讨论】:

  • app.listen(process.env.PORT || 3000, ..) 而不是 8000
  • 这个答案可以帮助你stackoverflow.com/questions/40714583/…
  • 你可以按照@YevgenGorbunkov 的建议去做。但问题是,一旦您点击npm start,您会在终端中看到类似“端口 3000 正在使用中,您是否要打开其他端口”之类的信息。
  • 谢谢,但我也需要 8000。是否可以以某种方式为两个端口配置,一个用于后端,一个用于前端?

标签: javascript reactjs express routes react-router


【解决方案1】:

谢谢大家的回答。我只需在客户端和公用文件夹中启动它即可运行该应用程序,后端运行在 8000 上,前端运行在 3000 上

【讨论】:

    【解决方案2】:

    您可以通过更改端口地址来使用任何端口 || 8000

    app.listen(process.env.PORT || 8000(change the value to 3000 or any port number), () => {
      console.log('Server is running on port: 8000(change the value to 3000 or any port number)');
    });
    

    【讨论】:

      【解决方案3】:
      app.listen(process.env.PORT || 3000, () => {
      console.log('Server is running on port: 3000');
      });
      

      【讨论】:

        猜你喜欢
        • 2014-11-09
        • 2018-06-18
        • 1970-01-01
        • 2021-06-10
        • 2022-12-22
        • 2015-02-16
        • 2020-01-26
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多