簡體   English   中英

express-subdomain不重定向subdomain.localhost獲取請求

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

我正在使用一個稱為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;

我試圖通過以下網址訪問登錄子域:

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

任何澄清或協助表示贊賞。

這里是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子域的來源,尤其是其測試。

快樂的編碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM