在我正在尝试编写的应用程序中,主页面(http://localhost:8675)具有以下形式:
<form action='/?joinnew' method='post'>
<button>Start</button>
</form>
这是server.js中的代码:
http.createServer(function(request, response) {
var root = url.parse(request.url).pathname.split('/')[1];
if (root == '') {
var query = url.parse(request.url).search:
if (query == '?joinnew') {
var newRoom = getAvaliableRoomId(); // '8dn1u', 'idjh1', '8jm84', etc.
// redirect the user's web browser to a new url
// ??? How to do. Need to redirect to 'http://whateverhostthiswillbe:8675/'+newRoom
...
}}}
我很乐意,如果有办法在我不需要知道主机地址的情况下这样做,因为这可能会改变。
'http'对象是常规需求('http'),不需要('express')。
答案 0 :(得分:101)
response.writeHead(301,
{Location: 'http://whateverhostthiswillbe:8675/'+newRoom}
);
response.end();
答案 1 :(得分:73)
http.get('*',function(req,res){
res.redirect('http://mydomain.com'+req.url)
})
答案 2 :(得分:31)
response.writeHead(301, {
Location: "http" + (request.socket.encrypted ? "s" : "") + "://" +
request.headers.host + newRoom
});
response.end();
答案 3 :(得分:2)
在Express中,您可以使用
res.redirect('http://example.com');
从服务器重定向用户。
要包含状态代码301或302,可以使用
res.redirect(301, 'http://example.com');
答案 4 :(得分:0)
您可以使用res.render()或res.redirect()方法通过node.js express重定向到另一个页面
例如:
var bodyParser = require('body-parser');
var express = require('express');
var navigator = require('web-midi-api');
var app = express();
app.use(express.static(__dirname + '/'));
app.use(bodyParser.urlencoded({extend:true}));
app.engine('html', require('ejs').renderFile);
app.set('view engine', 'html');
app.set('views', __dirname);
app.get('/', function(req, res){
res.render("index");
});
//This reponds a post request for the login page
app.post('/login', function (req, res) {
console.log("Got a POST request for the login");
var data = {
"email": req.body.email,
"password": req.body.password
};
console.log(data);
//Data insertion code
var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/";
MongoClient.connect(url, function(err, db) {
if (err) throw err;
var dbo = db.db("college");
var query = { email: data.email };
dbo.collection("user").find(query).toArray(function(err, result) {
if (err) throw err;
console.log(result);
if(result[0].password == data.password)
res.redirect('dashboard.html');
else
res.redirect('login-error.html');
db.close();
});
});
});
// This responds a POST request for the add user
app.post('/insert', function (req, res) {
console.log("Got a POST request for the add user");
var data = {
"first_name" : req.body.firstName,
"second_name" : req.body.secondName,
"organization" : req.body.organization,
"email": req.body.email,
"mobile" : req.body.mobile,
};
console.log(data);
**res.render('success.html',{email:data.email,password:data.password});**
});
//make sure that Service Workers are supported.
if (navigator.serviceWorker) {
navigator.serviceWorker.register('service-worker.js', {scope: '/'})
.then(function (registration) {
console.log(registration);
})
.catch(function (e) {
console.error(e);
})
} else {
console.log('Service Worker is not supported in this browser.');
}
// TODO add service worker code here
if ('serviceWorker' in navigator) {
navigator.serviceWorker
.register('service-worker.js')
.then(function() { console.log('Service Worker Registered'); });
}
var server = app.listen(63342, function () {
var host = server.address().host;
var port = server.address().port;
console.log("Example app listening at http://localhost:%s", port)
});
在登录部分,如果数据库中的电子邮件和密码匹配,则将网站定向到dashbaord.html,否则我们将使用res.redirect()方法显示page-error.html。您也可以使用res.render()在node.js中渲染页面
答案 5 :(得分:0)
如果您使用的是Express
,最干净的完整答案就是这个
const express = require('express')
const app = express()
app.get('*', (req, res) => {
// REDIRECT goes here
res.redirect('https://www.YOUR_URL.com/')
})
app.set('port', (process.env.PORT || 3000))
const server = app.listen(app.get('port'), () => {})