【问题标题】:express-subdomain not redirecting subdomain.localhost get requestexpress-subdomain 不重定向 subdomain.localhost 获取请求
【发布时间】:2015-02-26 23:59:30
【问题描述】:

我正在使用一个名为 express-subdomain 的 express.js 包来促进对我设置的已定义子域的请求。

据我所知,子域构造函数需要一个快速路由器对象,我从导出的路由器模块传递给它。

我尝试过的如下:

主 APP.JS 服务器文件

var common = {
    express: require('express'),
    subdomain: require('express-subdomain')

};

common.app = common.express();

module.exports = common;

common.app.listen(3000, function () {
  console.log(('app listening on http://localhost:3000'));
});

var router = require('./router/index');


// Error Handling
common.app.use(function(err, req, res, next) {
    res.status(err.status || 500);
});

路由器/索引

 module.exports = function (){
        var common = require('../app');
        var router = common.express.Router();

        common.app.get('/', function(req, res) {
        res.send('Homepage');
        });


        common.app.use('/signup', require('./routes/signup'));
        common.app.use(common.subdomain('login', require('./routes/login')));
    }();

路线/登录

var express = require('express');
var router = express.Router();


router.get('/', function (req, res) {
    res.send('login working');
});


router.get('/info', function (req, res) {

});
module.exports = router;

我已尝试通过以下 url 访问登录子域:

http://login.localhost
http://login.localhost:3000
http://login.localhost.com
http://login.localhost.com:3000

任何澄清或帮助表示赞赏。

【问题讨论】:

  • 向 login.localhost 发出请求时当前发生了什么?另外,当您说“已定义”时,您的意思是您已在 /etc/hosts 文件中明确写入域吗?

标签: javascript node.js express subdomain


【解决方案1】:

这里是 express-subdomain 的作者 ?

有几点:

  1. 必须正确设置主机 - 我建议在 /etc/hosts 文件中设置类似的内容。

127.0.0.1 myapp.local 127.0.0.1 login.myapp.local 有关这方面的更多信息,请参阅https://github.com/bmullan91/express-subdomain#developing-locally

  1. 先注册子域路由,包括主页路由。顺序很重要

  2. 不建议使用您在 /routes/index.js 中使用的模式(需要自调用函数)。像您在/routes/login.js 中所做的那样导出路由器更干净。

最后,如果您仍然遇到问题,请查看 express 子域的来源,尤其是它的测试。

愉快的编码。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2019-01-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-20
  • 2021-09-06
相关资源
最近更新 更多