【问题标题】:openshift, node.js: "Cannot GET /index.html"openshift,node.js:“无法获取 /index.html”
【发布时间】:2016-01-14 20:07:53
【问题描述】:

我在 Openshift 上有应用程序(Node.js 0.10,MongoDB 2.4)

我已按照此处的说明进行操作
developer.openshift.com/en/getting-started-debian-ubuntu.html
我已经成功修改了我的应用程序。
当我从这个 URL http://kunsento-mbtest1.rhcloud.com/ 运行它时
它工作正常。你可以试试。
如果我尝试使用此 URL http://kunsento-mbtest1.rhcloud.com/index.html
我收到“无法获取 /index.html”

如果我尝试访问子目录中的任何其他 html 文件,我会遇到同样的错误。

你能建议如何解决它吗?

谢谢

附言我在这里找到了类似的票;

openshift node.js Cannot Get /

但我不明白如何应用解决方案“您确定提交公用文件夹吗?”

【问题讨论】:

    标签: node.js openshift


    【解决方案1】:

    当您检查 OpenShift 的 nodejs 插件附带的 server.js 时,您会看到那里定义了 / 路由来服务索引文件:

    self.routes['/'] = function(req, res)
    {
        res.setHeader('Content-Type', 'text/html');
        res.send(self.cache_get('index.html') );
    };
    

    ...因此,当您访问您的域时,您将获得索引文件。

    路由通常是为应由 Web 应用程序提供的任何其他功能创建的。

    如果您想提供静态文件,您可以创建一个类似于the example you have linked 的公共目录。
    假设您使用的是 nodejs 插件附带的server.js,您只需将self.app.use(express.static(__dirname + '/public')); 添加到initializeServer 函数中即可获得public 目录的内容。为了清楚起见,这是整个块的样子:

    /**
     *  Initialize the server (express) and create the routes and register
     *  the handlers.
     */
    self.initializeServer = function() {
        self.createRoutes();
        self.app = express.createServer();
    
        //  Add handlers for the app (from the routes).
        for (var r in self.routes) {
            self.app.get(r, self.routes[r]);
        }
    
        // static files
        self.app.use(express.static(__dirname + '/public'));
    };
    

    您可能还想查看this 了解更多详情。

    【讨论】:

    • 您好,Jiri,感谢您的建议。我已尝试应用该解决方案(请参阅此处drive.google.com/file/d/0B434Tt3wD5AiNGNsclUwVG5wMTQ/…,但消息“Cannot GET /public/index.html”仍然存在。我的 Node.js 项目的 URL nodejs-mbtest1.rhcloud.com/public/index.html
    • 我认为它有效,您的静态文件 index.html 似乎位于nodejs-mbtest1.rhcloud.com/index.html。请注意,以这种方式访问​​静态文件时,不要使用目录路径 (/public)。附带说明一下,您通常会在 public 目录中创建一些子目录(如图像、js、css ..),并使用它来分隔不同的资源,然后可以在相应的目录下进行相对访问。跨度>
    猜你喜欢
    • 2021-12-26
    • 2014-05-31
    • 1970-01-01
    • 2013-01-19
    • 1970-01-01
    • 2019-06-11
    • 2011-10-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多