我是 react-router、express 和其他东西的新手。我一直在关注一些关于如何在 React 应用程序中刷新页面时防止“无法获取 URL”的教程。
我发现了很多关于这个问题的相关问答主题并尝试了一下。但是,不幸的是..它失败了。也许是因为 webpack 的版本已经升级到v3.5.5
我现在使用的版本。
我尝试使用webpack-dev-server
并将 historyApiFallback 属性放在 webpack 配置文件的 devServer 对象中。这也是一次失败。
所以,我会在下面提供一些配置文件。请检查并告诉我我做错了什么或者我应该做什么。
1) webpack.config.js
const path = require('path');
module.exports = {
entry: './client/src/index.jsx',
output: {
path: path.resolve(__dirname, '/client/dist/js'),
filename: 'app.js',
publicPath: './client/dist/js'
},
devServer: {
historyApiFallback: {
index: path.resolve(__dirname, './server/static/index.html'),
}
},
module: {
rules: [
{
test: /\.jsx?$/,
include: path.join(__dirname, '/client/src'),
use: 'babel-loader',
query: {
presets: ["react", "es2015"],
plugins: ["transform-class-properties"]
}
},
{
test: /\.css$/,
loader: 'style-loader!css-loader'
}
]
}
};
2) index.js/服务器文件
const express = require('express');
const bodyParser = require('body-parser');
const passport = require('passport');
const config = require('./config');
// connect to the database and load models
require('./server/models').connect(config.dbUri);
const app = express();
// tell the app to look for static files in these directories
app.use(express.static('./server/static/'));
app.use(express.static('./client/dist/'));
// tell the app to parse HTTP body messages
app.use(bodyParser.urlencoded({ extended: false }));
// pass the passport middleware
app.use(passport.initialize());
// load passport strategies
const localSignupStrategy = require('./server/passport/local-signup');
const localLoginStrategy = require('./server/passport/local-login');
passport.use('local-signup', localSignupStrategy);
passport.use('local-login', localLoginStrategy);
// pass the authenticaion checker middleware
const authCheckMiddleware = require('./server/middleware/auth-check');
app.use('/api', authCheckMiddleware);
// routes
const authRoutes = require('./server/routes/auth');
const apiRoutes = require('./server/routes/api');
app.use('/auth', authRoutes);
app.use('/api', apiRoutes);
// start the server
app.listen(3000, () => {
console.log('Server is running on http://localhost:3000');
});
3) 包.json
{
"name": "example-app",
"version": "1.0.0",
"description": "Example App",
"main": "index.js",
"scripts": {
"start": "nodemon --use_strict index.js",
"bundle": "webpack"
},
"license": "ISC",
"dependencies": {
"bcrypt": "^1.0.2",
"body-parser": "^1.17.2",
"bootstrap": "^4.0.0-alpha.6",
"express": "^4.15.4",
"jsonwebtoken": "^7.4.3",
"material-ui": "^0.19.0",
"mongoose": "^4.11.7",
"passport": "^0.4.0",
"passport-local": "^1.0.0",
"react": "^15.6.1",
"react-addons-css-transition-group": "^15.6.0",
"react-addons-transition-group": "^15.6.0",
"react-dom": "^15.6.1",
"react-router-dom": "^4.2.2",
"react-tap-event-plugin": "^2.0.1",
"reactstrap": "^4.8.0",
"validator": "^8.0.0"
},
"devDependencies": {
"babel-core": "^6.26.0",
"babel-loader": "^7.1.2",
"babel-plugin-transform-class-properties": "^6.24.1",
"babel-preset-es2015": "^6.24.1",
"babel-preset-react": "^6.24.1",
"clean-webpack-plugin": "^0.1.16",
"css-loader": "^0.28.5",
"file-loader": "^0.11.2",
"html-webpack-plugin": "^2.30.1",
"nodemon": "^1.11.0",
"style-loader": "^0.18.2",
"url-loader": "^0.5.9",
"webpack": "^3.5.5",
"webpack-dev-server": "^2.7.1"
}
}