node.jsreactjsexpressundefinedpublic-html

I dont know why my app is returning undefined


When I add this code to my app.js in the express server

app.get('/*', function (req, res) {
res.sendFile(path.join(publicPath, 'index.html')), function(err) {             
   if (err) {                 
       res.status(500).send(err) 
       }        
    };
 })

The console print the following error when I make a request, except fot the signin page which works good.

    Type error: cannot read
    react-dom.production.min.js:216 
    
    TypeError: Cannot read properties of undefined (reading '0')
    at Resultados.js:2368:16

Without adding that code lines, the app works okey, except when I refresh the page, because in the browser appear the message cannot get.

Why without the code the app is not returning undefined?


Solution

  • res.sendFile only sends index.html file and other files in your public folder are inaccessible. Serve your public folder like this

    const path = require('path')
    const publicFolder = path.join(__dirname, 'public');
    app.use('/*', express.static(publicFolder))