【发布时间】: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