2016-01-08 21 views
0

我申請上Openshift(Node.js的0.10,MongoDB的2.4)openshift,Node.js的:這裏 「不能得到/index.htm」 明明

我按照說明書
developers.openshift.com/en/啓動-debian-ubuntu.html
和我已經修改成功我的應用程序。
當我從這個URL運行它http://kunsento-mbtest1.rhcloud.com/
它工作正常。你可以嘗試一下。
如果我嘗試使用這個網址http://kunsento-mbtest1.rhcloud.com/index.html
我得到「無法得到/index.htm」明明

我得到同樣的錯誤,如果我嘗試訪問其他任何HTML文件中的子目錄。

請你能建議如何解決它?

謝謝

p.s.我在這裏找到類似的票;

openshift node.js Cannot Get /

,但我不明白如何應用解決方案「是你一定要提交公用文件夾?」

回答

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更多的細節。

+0

你好吉日, 感謝您的建議。我試圖應用該解決方案(請參閱 https://drive.google.com/file/d/0B434Tt3wD5AiNGNsclUwVG5wMTQ/view?usp=sharing 但消息「無法GET /public/index.html」仍然存在。 我的Node.js項目的URL http://nodejs-mbtest1.rhcloud.com/public/index.html –

+0

我認爲它的工作原理,你的靜態文件index.html似乎在http://nodejs-mbtest1.rhcloud .com/index.html。請注意,當以這種方式訪問​​靜態文件時,您不需要使用目錄路徑('/ public')。在附註中,您通常會在'public'目錄中創建一些子目錄(像圖片,js,css,..),並用它來分離不同的資源,然後可以在相應的目錄下相對訪問。 –

+0

你是對的,它的工作原理非常感謝! –