Node.js Nodejs/Express-子域自动重定向到根域

Node.js Nodejs/Express-子域自动重定向到根域,node.js,express,Node.js,Express,嘿,我创建了一个带有电子邮件确认的小注册脚本,我想在这里使用express subdomain。除确认部分外,一切正常。如果我单击确认电子邮件中的链接,它会将我重定向到确认子域()。。。这就是问题所在。服务器应接收Id为的get请求并更新数据库: loginapp.get('/reg/confirm', function (req, res) { var unid = req.query.id; console.log('before update'); db.confirmAcco

嘿,我创建了一个带有电子邮件确认的小注册脚本,我想在这里使用express subdomain。除确认部分外,一切正常。如果我单击确认电子邮件中的链接,它会将我重定向到确认子域()。。。这就是问题所在。服务器应接收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');
  });
})
但它只是自动将我重定向到,甚至不需要输入代码(控制台中没有输出,数据库也不会更新)

子域配置:

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子域的缘故?

我用了你的代码并做了一个小测试。我必须修改我的
/etc/hosts
(Windows:
c:\Windows\System32\drivers\etc\hosts
)如下:

127.0.0.1   myapp.com
127.0.0.1   login.myapp.com
您的代码(在这里,我将其全部移动到一个文件中):


这工作得很好(当然我得到了一个DB错误,但我可以看到它处理正确的路线)!所以我想,代码本身并没有什么问题…

我只是重命名了get请求的路径:

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');
 });
})


现在一切正常了…

这个
else
在其范围内没有
if
的情况下挂起是有问题的。你确定这在语法上是有效的吗?哦,是的,修正了它,但仍然是相同的结果。就像我说的,如果我打开网站,它甚至不会运行代码…你能修复问题中包含的代码片段吗?嗯,我已经用get或post方法检查过你的路由器了,就像你在这里提到的一样。嗯,谢谢你在你这边测试它!但我还是想知道这个问题是从哪里来的。。。如果它不是代码本身,那么它会是什么呢?你能试一下上面的代码吗。然后我们至少可以看看它是否有效,或者它是否超出了提供的代码范围。好吧,我试了好几次,并将所有内容都记录在控制台中,到目前为止它似乎有效…哦,哇,看起来我解决了它。。。刚刚将路径从
/reg/confirm
更改为
/reg/con
,现在它工作了…并解释了这一点?嗯,不知道,对不起。。。如果我能找出问题所在,我会加上它!
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');
 });
})