Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/42.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 反应+;Express无法获取/登录_Node.js_Reactjs_Express_Babeljs_Babel Polyfill - Fatal编程技术网

Node.js 反应+;Express无法获取/登录

Node.js 反应+;Express无法获取/登录,node.js,reactjs,express,babeljs,babel-polyfill,Node.js,Reactjs,Express,Babeljs,Babel Polyfill,我知道这方面有很多问题,但没有一个能解决我的问题。 我有一个带有react路由器的react客户端应用程序,当用户无法登录我的express时,我有 app.post('/login', passport.authenticate('local', { successRedirect: '/', failureRedirect: '/login', failureFlash: true })) 因此,用户保持/login状态,但当页面刷新时,出现错误无法获取/login

我知道这方面有很多问题,但没有一个能解决我的问题。 我有一个带有react路由器的react客户端应用程序,当用户无法登录我的express时,我有

app.post('/login', passport.authenticate('local', {
    successRedirect: '/',
    failureRedirect: '/login',
    failureFlash: true
}))
因此,用户保持/login状态,但当页面刷新时,出现错误
无法获取/login
。 在网上做了很多研究,其中一些人说要创建一个通配符路由,如下所示:

 app.use(express.static(path.join(__dirname, '/client/build')));
 app.get('*', (req,res) =>{
    res.sendFile(path.join(__dirname, 'client/build/index.html'));
 }); // this is below all other routes
require('babel-register')({
    presets: [ 'env', 'es2015', 'react' ]
})
require("babel-polyfill")

module.exports = require('./index.js');
const connectHistoryApiFallback = require('connect-history-api-fallback');

... // require other libraries...
... // app.use( other middleware ...

app.use(connectHistoryApiFallback({
  verbose: false
}));

// static files and folders must be set after connectHistoryApiFallback
app.use(express.static(path.join(__dirname, '/client/build')));

... // other routes 

app.post('/login', passport.authenticate('local', {
  successRedirect: '/',
  failureRedirect: '/login',
  failureFlash: true
}))

// app.listen...
在webpack.config.dev.js中添加了以下代码:

devServer: {
   historyApiFallback: true
}
但在那之后,我在浏览器控制台的1.fce49d06.chunk.js:1中获得了
意外标记。之后,阅读关于
babel register
以传输我的节点代码,因此我创建了一个新文件
start.js
,如下所示:

 app.use(express.static(path.join(__dirname, '/client/build')));
 app.get('*', (req,res) =>{
    res.sendFile(path.join(__dirname, 'client/build/index.html'));
 }); // this is below all other routes
require('babel-register')({
    presets: [ 'env', 'es2015', 'react' ]
})
require("babel-polyfill")

module.exports = require('./index.js');
const connectHistoryApiFallback = require('connect-history-api-fallback');

... // require other libraries...
... // app.use( other middleware ...

app.use(connectHistoryApiFallback({
  verbose: false
}));

// static files and folders must be set after connectHistoryApiFallback
app.use(express.static(path.join(__dirname, '/client/build')));

... // other routes 

app.post('/login', passport.authenticate('local', {
  successRedirect: '/',
  failureRedirect: '/login',
  failureFlash: true
}))

// app.listen...
在服务器的package.json中,我还有

"scripts": {
   "start": "node start.js",
   "server": "nodemon start.js",
   "client": "npm run start --prefix client",
   "dev": "concurrently \"npm run server\" \"npm run client\""
},
但仍然存在意外的标记<

有什么想法吗?

使用连接历史api回退中间件

您可以使用中间件始终回退到index.html。这可以在dev和prod环境中使用

像这样使用它:

 app.use(express.static(path.join(__dirname, '/client/build')));
 app.get('*', (req,res) =>{
    res.sendFile(path.join(__dirname, 'client/build/index.html'));
 }); // this is below all other routes
require('babel-register')({
    presets: [ 'env', 'es2015', 'react' ]
})
require("babel-polyfill")

module.exports = require('./index.js');
const connectHistoryApiFallback = require('connect-history-api-fallback');

... // require other libraries...
... // app.use( other middleware ...

app.use(connectHistoryApiFallback({
  verbose: false
}));

// static files and folders must be set after connectHistoryApiFallback
app.use(express.static(path.join(__dirname, '/client/build')));

... // other routes 

app.post('/login', passport.authenticate('local', {
  successRedirect: '/',
  failureRedirect: '/login',
  failureFlash: true
}))

// app.listen...

所以,问题基本上是页面何时刷新?您的所有路由都会发生这种情况(刷新,然后出现get错误),还是仅在登录重定向中发生?目前仅使用/login,因为只有它具有节点重定向
故障重定向:'/login',
我的其他路由目前仅在react router中前端的路由器是否处理
/login
路由?是的。我认为问题来自发送index.htmld的通配符路由。它是否意味着删除通配符路由?使用此中间件,您不需要通配符路由。如果找不到index.html,请尝试使用来自文档的设置其位置。不工作…再次使用相同的意外标记。如果我删除这个
app.use(express.static(path.join(uu dirname,“/client/build”))
然后意外的令牌错误消失,但无法获取/login is logged在不更改
webpack.config.dev.js
babel注册表的情况下尝试此解决方案。。。是的,我删除了
devServer:{historyApiFallback:true}
并删除了babel,所以现在我的主服务器文件是index.js not start.js(带有babel的文件)和意外的标记