我是 NodeJS 和 Express 的新手,这就是我想做的:
检查连接值后,我想在用户的主页上重定向我的用户。我不想更改 URL,只是显示另一个 html 页面。
这是代码,以及我想放置重定向的位置:
var express = require('express');
var path = require('path');
var app = express();
var server = require('http').Server(app);
var io = require('socket.io').listen(server);
server = server.listen(8080);
app.get('/', function (req, res) {
res.sendFile(__dirname + '/public/login.html');
});
app.use(express.static(path.join(__dirname, '/public')));
io.on('connection', function (socket) {
socket.on('connexion_client', function(data) {
if(input_are_ok) {
// When the client is connected & his input are checked
// HERE: I want to redirect to 'public/index.html'
}
}); // socket.on
}); // io.on
谢谢。
最佳答案
您可以像这样使用res.redirect([status,] path)
:
if (input_are_ok) {
res.redirect('public/index.html');
}
这将使用指定的 HTTP 状态代码状态重定向到给定的 URL。如果未指定状态代码,则状态代码默认为 302(这在大多数用例中都可以)。更多信息请参见official documentation .
关于node.js - 重定向到另一个 html 页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33942334/