Sunday, 14 May 2017

Can not get correct static files after refreshing except index page

When I refresh page on index route(/), and login page (/login), it works fine.
However, my web site gets error as I refresh on other routes, for example, /user/123456.
Because no matter what the request is, the browser always get html file.
Thus, both of the content in main.css and main.js are html, and the browser error.
screen shot
I have already read the README of create-react-app.
Whether I use serve package or express, it will produce the strange bug.

Following is my server code.

//server.js
const express = require('express');
const path = require('path');
app.use(express.static(path.join(__dirname, 'build')));
app.get('/*', (req, res) => {
  res.sendFile(path.join(__dirname, 'build', 'index.html'));
});
const PORT = process.env.PORT || 80;
app.listen(PORT, () => {
  console.log(`Production Express server running at localhost:${PORT}`);
});



via Zhao Shou Hao

No comments:

Post a Comment