[英]Nodejs/Express - subdomain automatically redirects to root domain
嘿,我創建了一個帶有電子郵件確認的小型注冊腳本,我想在這里使用express-subdomain。 除確認部分外,其他一切正常。 如果我單擊確認電子郵件中的鏈接,它會將我重定向到確認子域( http://login.myapp.com:5000/reg/confirm?id=xxx )...這就是問題所在。 服務器應接收帶有ID的get請求並更新數據庫:
loginapp.get('/reg/confirm', function (req, res) {
var unid = req.query.id;
console.log('before update');
db.confirmAccount(unid, function (error, unid) {
if (error) throw error;
console.log('after update');
res.redirect('http://myapp.com:5000');
});
})
但是相反,它只是自動將我重定向到http://myapp.com:5000 ,甚至無需輸入該代碼(控制台和數據庫中的任何輸出都不會得到更新)。
子域配置:
const express = require('express');
const subdomain = require('express-subdomain');
var app = express();
var loginapp = express.Router();
app.use(subdomain('login', loginapp));
app.listen(5000, function () {
console.log('+++ LISTENING ON PORT 5000 +++');
});
我的代碼有問題還是由於express-subdomain引起的?
我接受了您的代碼並進行了一些測試。 我必須通過添加以下內容來修改我的/etc/hosts
(Windows: c:\\Windows\\System32\\drivers\\etc\\hosts
)。
127.0.0.1 myapp.com
127.0.0.1 login.myapp.com
您的代碼(這里我將其全部移動到一個文件中):
const express = require('express');
const subdomain = require('express-subdomain');
var app = express();
var loginapp = express.Router();
loginapp.get('/', function (req, res) {
res.send('Login is working');
console.log('LOGIN');
})
loginapp.get('/reg/confirm', function (req, res) {
var unid = req.query.id;
console.log('before update');
console.log(unid);
db.confirmAccount(unid, function (error, unid) {
if (error) throw error;
console.log('after update');
res.redirect('http://myapp.com:5000');
});
})
app.use(subdomain('login', loginapp));
app.listen(5000, function () {
console.log('+++ LISTENING ON PORT 5000 +++');
});
這工作得很好(當然,我遇到數據庫錯誤,但我可以看到它解決了正確的路線)! 所以我猜想,代碼本身並不是真正的問題...
我剛剛從以下位置重命名了獲取請求的路徑:
loginapp.get('/reg/confirm', function (req, res) {
var unid = req.query.id;
console.log('before update');
console.log(unid);
db.confirmAccount(unid, function (error, unid) {
if (error) throw error;
console.log('after update');
res.redirect('http://myapp.com:5000');
});
})
至
loginapp.get('/reg/con', function (req, res) {
var unid = req.query.id;
console.log('before update');
console.log(unid);
db.confirmAccount(unid, function (error, unid) {
if (error) throw error;
console.log('after update');
res.redirect('http://myapp.com:5000');
});
})
現在一切正常...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.