chosirai
chosirai

Reputation: 193

Node express always return index.html on debian

I have node express server, the server working correctly on windows system. But on debian always return index.html.

When I go to localhost:port/ its retrun index.html, index.html load some js files and images(fav ico) - content of this files is ALWAYS content of index.html... so.. where is a problem?

code:

const express = require('express')
const path = require('path')
const application = express()
const port = process.env.PORT || 80

const PUBLIC_DIR = 'public'

application.use(express.static(path.join(__dirname, PUBLIC_DIR)))
application.listen(port)

application.use(express.static('client/build'));  //use your build path my build path under the root folder is client/build
  const path = require('path');
  application.get('*', (req, res) => {
    res.sendFile(path.resolve(__dirname, 'client','build', 'index.html')); //use your build path my build path under the root folder is client/build
  });

//handle 404
application.use((req, res) => {
    res.send('404: Page not Found', 404)
});

//handle 500
application.use((error, req, res, next) => {
    res.send('500: Internal Server Error', 500)
});

console.log(['HTTP server running on ', process.env.HOST, ' / ', port].join(''))

Upvotes: 0

Views: 336

Answers (2)

lame_coder
lame_coder

Reputation: 3115

Change following line

app.use(express.static('client/build'));  //use your build path my build path under the root folder is client/build

to

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

So app will get proper path of the build folder and will not fall in catch-all route for static resource requests.

Upvotes: 1

fh0592x
fh0592x

Reputation: 286

You declare your express instance as const application = express() but then you're using app.use on line 11. Maybe changing it to application.use will provide better results?

Upvotes: 1

Related Questions