英文:
how to redirect to another page from a node.js server
问题
这是Node.js服务器的代码,你想要在这里进行重定向。但是要注意,Node.js的http
模块本身不提供直接的res.redirect
函数。你需要使用res.writeHead
和res.end
来手动发送一个重定向响应。以下是代码的修改部分:
http.createServer(function (req, res) {
var urlQuery = url.parse(req.url, true).query;
removeItem(urlQuery.itemToRemove);
addItem(urlQuery.itemToAdd);
// 手动进行重定向
res.writeHead(302, {
'Location': '/index.html' // 重定向到index.html或你想要的URL
});
res.end();
}).listen(8080);
这里我用res.writeHead
设置了状态码为302,表示重定向,然后在响应头中设置了重定向的目标URL。这样,当请求到达服务器时,它将重定向到指定的URL。
英文:
i have been struggling for the pas few days i want to redirect to another page from my node.js server.
first by website is redirecting me to my server to do a task but when that task is done i want the server to redirect me back to my website.
here is the node js server code.
var http = require('http');
var url = require('url');
var MongoClient = require('mongodb').MongoClient;
var mongourl = "mongodb://localhost:27017/";
http.createServer(function (req, res) {
res.writeHead(200, {
'Content-Type': 'text/html'
});
var urlQuery = url.parse(req.url, true).query;
removeItem(urlQuery.itemToRemove);
addItem(urlQuery.itemToAdd);
**// i have been trying to redirect to my html site **
**// i have got a error saying TypeError: res.redirect is not a function**
res.redirect('index.html');
res.end();
}).listen(8080);
function removeItem(remove) {
if (remove !== undefined) {
MongoClient.connect(mongourl, function (err, db) {
if (err) throw err;
var dbo = db.db("mydb");
var myquery = {
itemName: remove
};
dbo.collection("shoppingCart").deleteOne(myquery, function (err, obj) {
if (err) throw err;
console.log("deleted:" + myquery);
db.close();
});
});
}
}
function addItem(create) {
if (create !== undefined) {
MongoClient.connect(mongourl, function (err, db) {
if (err) throw err;
var dbo = db.db("mydb");
var myobj = {
itemName: create
};
dbo.collection("shoppingCart").insertOne(myobj, function (err, res) {
if (err) throw err;
console.log("document inserted:" + myobj);
db.close();
});
});
}
}
答案1
得分: 0
你遇到该错误是因为 response.redirect() 是一个 Express 函数,而你的应用程序纯粹基于 Node.js 的 http 库。
你可以通过以下方式实现你所提到的功能:
res.writeHead(301, { Location: 'your-location' });
res.end();
英文:
You are getting that error because response.redirect() is an express function and your app is based purely on nodejs http library.
You can accomplish what you have mentioned this way:
res.writeHead(301,{Location: 'your-location'});
res.end();
答案2
得分: 0
你没有正确地创建服务器。尽量保持代码整洁,编写单独的API用于提供index.html,不要将其放在创建服务器的内部。
英文:
You are not creating the server properly. Try to keep your code clean,Write separate API for serving index.html, not inside creating the server.
答案3
得分: 0
> TypeError: res.redirect is not a function
这是因为 res.redirect
是一个 Express 函数,而您正在使用纯粹的 http
节点服务器。
您可以考虑使用 express
来处理重定向。
var http = require('http');
var url = require('url');
var MongoClient = require('mongodb').MongoClient;
var mongourl = 'mongodb://localhost:27017/';
// express
const express = require('express');
const app = express();
const port = 8080;
// 用于模板化
// 创建 `views/` 文件夹
// 并放置 `index.pug` 和 `redirected-page.pug`
app.set('view engine', 'pug');
app.get('/redirect', (req, res) => {
res.render('redirected-page');
});
app.get('/', (req, res) => {
var urlQuery = url.parse(req.url, true).query;
Promise.all([
removeItem(urlQuery.itemToRemove),
addItem(urlQuery.itemToAdd),
]).then(() => {
res.redirect('redirect');
});
});
app.use(function(err, req, res, next) {
console.error(err.stack);
res.status(500).send('Something broke!');
});
function removeItem(remove) {
if (remove !== undefined) {
MongoClient.connect(mongourl, function(err, db) {
if (err) throw err;
var dbo = db.db('mydb');
var myquery = {
itemName: remove,
};
dbo.collection('shoppingCart').deleteOne(myquery, function(err, obj) {
if (err) throw err;
console.log('deleted:' + myquery);
db.close();
});
});
}
}
function addItem(create) {
if (create !== undefined) {
MongoClient.connect(mongourl, function(err, db) {
if (err) throw err;
var dbo = db.db('mydb');
var myobj = {
itemName: create,
};
dbo.collection('shoppingCart').insertOne(myobj, function(err, res) {
if (err) throw err;
console.log('document inserted:' + myobj);
db.close();
});
});
}
}
app.listen(port, () => console.log(`Server is running at ${port}!`));
英文:
> TypeError: res.redirect is not a function
This is because res.redirect
is an express function and you're using plain http
node server.
You can consider using express
for handling redirects for you.
var http = require('http');
var url = require('url');
var MongoClient = require('mongodb').MongoClient;
var mongourl = 'mongodb://localhost:27017/';
// express
const express = require('express');
const app = express();
const port = 8080;
// for templating
// create `views/` folder
// and place `index.pug` and `redirected-page.pug`
app.set('view engine', 'pug');
app.get('/redirect', (req, res) => {
res.render('redirected-page');
});
app.get('/', (req, res) => {
var urlQuery = url.parse(req.url, true).query;
Promise.all([
removeItem(urlQuery.itemToRemove),
addItem(urlQuery.itemToAdd),
]).then(() => {
res.redirect('redirect');
});
});
app.use(function(err, req, res, next) {
console.error(err.stack);
res.status(500).send('Something broke!');
});
function removeItem(remove) {
if (remove !== undefined) {
MongoClient.connect(mongourl, function(err, db) {
if (err) throw err;
var dbo = db.db('mydb');
var myquery = {
itemName: remove,
};
dbo.collection('shoppingCart').deleteOne(myquery, function(err, obj) {
if (err) throw err;
console.log('deleted:' + myquery);
db.close();
});
});
}
}
function addItem(create) {
if (create !== undefined) {
MongoClient.connect(mongourl, function(err, db) {
if (err) throw err;
var dbo = db.db('mydb');
var myobj = {
itemName: create,
};
dbo.collection('shoppingCart').insertOne(myobj, function(err, res) {
if (err) throw err;
console.log('document inserted:' + myobj);
db.close();
});
});
}
}
app.listen(port, () => console.log(`Server is running at ${port}!`));
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论