2014-09-02 92 views
1

服務器啓動時沒有任何錯誤,但URL被擊中,顯示沒有應用程序正在運行。基本Node.js代碼不起作用

網址:link_To_Server

我的代碼:

var express= require('express'); 
var app=express(); 

app.get('/', function(req, res, next) { 
    console.log('home'); 
    res.send(200); 
    res.end('getting requests'); 
}); 
console.log('server started'); 
+0

這是您的整個代碼?如果是這樣,你還沒有調用'app.listen(port);' – 2014-09-02 09:22:25

回答

5

你實際上並沒有設置服務器監聽的端口。更換

console.log('server started'); 

隨着

app.listen(3000, function() { 
    console.log('server started'); 
}); 

注意,與上面的例子中,綁定端口是3000。欲瞭解更多信息,請參閱the API doc

+1

請記住,端口3000不會在Cloud9中工作,並且您需要使用$ PORT。在Node.js中,你可以使用'process.env.PORT'。 'process.env.IP'或使用0.0.0.0。 – lcipriani 2014-09-02 09:52:21

+0

非常感謝。請給我發一封郵件([email protected]),以便我可以親自與您保持聯繫。由於我是node.js和angular.js的新學員,能否請您指導我。提前感謝。 – Rajeshwar 2014-09-02 10:29:57