Nodejs, express, browser does not find files

Asked

Viewed 565 times

0

Good afternoon guys, I made an API with Node and I consumed it with React, in development environment, works OK because I start one independent of the other, but in production I’m trying to deliver the static files compiled by React to Node, are 3 files. HTML, CSS and JS. HTML is being delivered perfectly, however JS and CSS do not seem to be Oks.

In this case, the HTML is being delivered because the title and icon are working, but the screen turns white and in the console appears the following message: "Failed to load with source "http://biodinamicahc.com.br/app.js"

In case, I’m handing over the public folder of the second form:

if (process.env.PRODUCTION) {
    app.set('public', path.resolve(__dirname, '../../public'));
    app.use(express.static(app.get('public')));
}

For when the "/" route is accessed, it plays the other files together.

server.get('/', (req, res) => {
        if (process.env.PRODUCTION) {
            res.sendFile(path.resolve(__dirname, '../../../public/index.html'));
        } else {
            res.status(500).send({
                message: 'API rodando em ambiente de desenvolvimento!'
            })
        }

    });

I tried to play the static JS and CSS files together in "res.sendFile", but it didn’t work, the message continued. Would anyone know what to say?

Thank you!

1 answer

2

Using express

app.use(express.static("public"));

Suppose I have these folders

/
  /server.js
  /public
    /css
      /style.css
    /js
      /index.js
  /index.html

Within /index.html do this

<link rel="stylesheet" type="text/css" href="/css/style.css"></link>

Notice I didn’t put /public/css/style.css

The express interprets the /public as if it were the root of the application in the browser.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.