我是NodeJS和express的新手,下面是我想做的:
在检查connexion值之后,我想将我的用户重定向到他的主页。我不想更改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谢谢。
发布于 2015-11-27 02:52:10
您可以像这样使用res.redirect([status,] path):
if (input_are_ok) {
res.redirect('public/index.html');
}这将使用指定的HTTP状态码状态重定向到给定的URL。如果未指定状态码,则状态码默认为302 (这在大多数用例中都很好)。有关更多信息,请参阅official documentation。
https://stackoverflow.com/questions/33942334
复制相似问题