react前端的路由已经用BrowserRouter配置好了,3000端口运行也没问题,
npm run build 打包后放到java项目里,启动后,8080端口在首页后的跳转
出现404错误,网上搜了许多,说是后端没有配置路由,在8080跳转时会向服务器发送请求,后端没有配置,所以报404。
想问一下后端的路由我该怎么配置。
不用HashRouter,因为多个#。
springboot+react
https://blog.csdn.net/weixin_30781775/article/details/96107439
看了这个不知道可不可行,想试试,这段代码应该写在哪?没用过webpack,
// catch 404 and forward to error handler
app.use(function(req, res, next) {
//判断是主动导向404页面,还是传来的前端路由。
//如果是前端路由则如下处理
fs.readFile(__dirname + '/public/dist/index.html', function(err, data){
if(err){
console.log(err);
res.send('后台错误');
} else {
res.writeHead(200, {
'Content-type': 'text/html',
'Connection':'keep-alive'
});
res.end(data);
}
})
});
试着安装试了下结果就报错了
webpack.config.js
const path = require('path')
module.exports = {
entry: './src/index.js',
output: {
path: path.join(__dirname, 'dist'),
filename: 'bundle.js'
},
module: {
loaders: [{
test: /\.jsx?$/,
loader: 'babel'
}, {
test: /\.css$/,
loader: 'style!css'
}, {
test: /\.less$/,
loader: 'style!css!less'
},{
test: /\.(png|jpg)$/,
loader: 'url?limit=25000'
}]
}
}
错误内容
[webpack-cli] Compilation finished
assets by status 325 bytes [cached] 1 asset
./src/index.js 315 bytes [built] [code generated] [1 error]
ERROR in ./src/index.js 9:2
Module parse failed: Unexpected token (9:2)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
|
| ReactDOM.render(
> <BrowserRouter>
| <App />
| </BrowserRouter>,