简体   繁体   English

express-subdomain不重定向subdomain.localhost获取请求

[英]express-subdomain not redirecting subdomain.localhost get request

I am using an express.js package called express-subdomain to facilitate requests to defined subdomains I set up. 我正在使用一个称为express-subdomain的express.js包来简化对我设置的已定义子域的请求。

As far as I understand, the subdomain constructor function expects an express router object which I pass to it from an exported router module. 据我了解,子域构造函数需要一个导出的路由器模块传递给它的快速路由器对象。

What I have tried is as follows: 我尝试过如下:

MAIN APP.JS SERVER FILE 主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);
});

router/index 路由器/指数

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

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;

I have tried to access the login subdomain at the following urls: 我试图通过以下网址访问登录子域:

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

Any clarification or assistance appreciated. 任何澄清或协助表示赞赏。

author of express-subdomain here 👋 这里是express-subdomain的作者

A couple of things: 几件事情:

  1. Hosts must be setup correctly - I'd recommend something like so in your /etc/hosts file. 主机必须正确设置-我建议您在/ etc / hosts文件中添加类似内容。

127.0.0.1 myapp.local 127.0.0.1 login.myapp.local For more information on this see https://github.com/bmullan91/express-subdomain#developing-locally 127.0.0.1 myapp.local 127.0.0.1 login.myapp.local有关此的更多信息,请参见https://github.com/bmullan91/express-subdomain#developing-locally

  1. Register the subdomain routes before any others, including the homepage route. 先注册子域路由,再注册其他路由,包括首页路由。 The order is very important 顺序很重要

  2. The pattern you're using in /routes/index.js is not advised (requiring a self invoking function). 不建议您在/routes/index.js使用的模式(需要自调用功能)。 Exporting the Router like you done in /routes/login.js is cleaner. 像在/routes/login.js一样完成导出路由器的操作,会更干净。

Finally, If you're still stuck take a look at the source for express subdomain and in particular its tests. 最后,如果您仍然不满意,请查看Express子域的来源,尤其是其测试。

Happy coding. 快乐的编码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM