【问题标题】:Problem refresh pages nodejs cannot /get error问题刷新页面nodejs不能/get错误
【发布时间】:2021-04-10 06:34:07
【问题描述】:

我已经建立了一个网站,当我在主页上并刷新它时很好,但是当我导航到其他页面时它也很好,但是当我在这些其他页面上刷新时

Cannot GET /otherpage

我正在使用 react-navigation,如果我超链接了它,我认为它可能也无法正常工作。


require('rootpath')();
const express = require('express');
const app = express();
///
///
app.use('/accounts', require('./accounts/accounts.controller'));

if(process.env.NODE_ENV === 'production'){
 
  app.use(express.static('build'));

 

  }
server.js
build

文件目录

我该如何解决这个问题?

【问题讨论】:

    标签: node.js reactjs express react-navigation production


    【解决方案1】:
    // Serve the static files from the React app
    app.use(express.static(path.join(__dirname, 'client/build')));
    
    // An api endpoint that returns a short list of items
    app.get('/api/getList', (req,res) => {
        var list = ["item1", "item2", "item3"];
        res.json(list);
        console.log('Sent list of items');
    });
    
    // Handles any requests that don't match the ones above
    app.get('*', (req,res) =>{
        res.sendFile(path.join(__dirname+'/client/build/index.html'));
    });
    

    请按照本教程进行进一步说明:https://dev.to/nburgess/creating-a-react-app-with-react-router-and-an-express-backend-33l3

    【讨论】:

      猜你喜欢
      • 2021-12-31
      • 1970-01-01
      • 1970-01-01
      • 2011-05-13
      • 2017-04-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多