简体   繁体   English

NodeJS 发送 index.js 文件作为响应

[英]NodeJS sending index.js file as response

So i built an app using NodeJS in the backend and ReactJs in the frontend and everything worked well while testing.因此,我在后端使用 NodeJS 并在前端使用 ReactJs 构建了一个应用程序,并且在测试时一切正常。 Now everytime i make a request in the production server, the request sends the index.html content instead of the data.现在每次我在生产服务器中发出请求时,请求都会发送 index.html 内容而不是数据。

Here is the content of server.js这是 server.js 的内容

//Some imports

app.use(
  bodyParser.urlencoded({
    extended: true,
    limit: "100mb",
    parameterLimit: 1000000,
  })
);
const port = 5000;

app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(cors());

app.use(express.static(path.join(__dirname, 'build')));

app.set("trust proxy", true);

...

app.post('/api/test', async (req, res) => {
  res.send("Hello world!")
});

//Sample of the nodejs response //nodejs响应的示例在此处输入图像描述

//Sample of a function that makes a request //发出请求的函数示例在此处输入图像描述

//This belongs to the call function that checks for the validity of the token!

        //Main Request
        await api.post(EndPoint, BodyObject, { headers: headers })
            .then(async function (response) {

                //A resposta da main pode ser 2 coisas
                //--A resposta do request com a data
                //--A resposta do /api/auth com a mensagem a dizer que o token está invalido  

                //Unsuccessful request
                if (response.data == "Access Token expired" || response.data == "There's something wrong with token") {
                    //Store invalid to call /newToken endPoint
                    tokenValidity = false;
                    //Unsuccessful request
                } else if (response.data == "Invalid Signature") {
                    //Redirect to Login
                    window.location.href = "https://www.link.pt/login";
                }
                //Successful request
                else {
                    serverResponse = response.data;
                }
            })

//Sample of an endpoint in server.js //server.js 中端点的示例在此处输入图像描述

app.get('/*', (req, res) => {res.sendFile(path.resolve(__dirname, 'build', 'index.html'));

you're sending the index.html file as response to all routes other than /api/test您正在发送 index.html 文件作为对除 /api/test 以外的所有路由的响应

I need to see the frontend code where you are making the request我需要查看您发出请求的前端代码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM