【问题标题】:baseUrl is not working in my MEAN stack app, How to fix this?baseUrl 在我的 MEAN 堆栈应用程序中不起作用,如何解决这个问题?
【发布时间】:2016-08-14 09:51:30
【问题描述】:

我已经使用 MEAN 堆栈创建了 ope 应用程序。以前一切正常,但是当我进入设置 baseUrl 时,它不能正常工作。

config.js

 module.exports = {

"database": "mongodb://localhost:27017/fuse",
"port": process.env.PORT || 7200,
"secretKey": "YourSecretKey",


baseUrl: 'http://192.168.2.8:3000',
setBaseUrl : function(url){
    this.baseUrl = url;
},
getBaseUrl : function(){
    return this.baseUrl;
}

}

app.js

var express    =    require('express');
var config = require('./config');
var mongoose = require('mongoose');
var url = require('url');
var http = require('http');


http.createServer(function (req, res) {
var hostname = req.headers.host; // hostname = 'localhost:7200'
console.log(hostname);
config.setBaseUrl(hostname);
var pathname = url.parse(req.url).pathname; // pathname = '/MyApp'
console.log('http://' + config.getBaseUrl() + pathname);

res.writeHead(200);
res.end();
}).listen(3000);

【问题讨论】:

    标签: node.js express


    【解决方案1】:

    嗯,你用的是express,为什么不用它来创建你的服务器呢?

    var app = express();
    
    var port = 3000;
    
    app.use(function(req, res, next) {
        req.getUrl = function() {
            return req.protocol + "://" + req.get('host') + req.originalUrl;
        }
        return next();
    });
    
    app.listen(port, function {
        console.log('Server running on port ' + port);
    
    });
    

    这样,您的 url 将始终包含在 req.getUrl 中。

    希望对你有帮助!

    【讨论】:

    • 我有 'FUSE' 主题,因为 gulp 在端口 3000 上运行。如果我使用相同端口创建节点,则会显示错误。
    • 你不能使用其他端口进行快递吗?喜欢 3001?
    • 哈哈,是的,我又使用了一个端口号,现在它可以工作了。谢谢。
    猜你喜欢
    • 2014-09-06
    • 2017-11-06
    • 1970-01-01
    • 2021-07-02
    • 1970-01-01
    • 2020-11-06
    • 2019-11-24
    • 2022-01-25
    • 1970-01-01
    相关资源
    最近更新 更多