2013-12-10 20 views
0

這是一個基本的應用程序我已經得到了一個蒙戈DB基本app + express + mongodb +如何不使用端口?

如果我去​​無論是在使用意見明確了建立和航線,也做一些查詢/ hello.html的將被顯示在瀏覽器。 如果我去http://localhost:8080/test'這是一個測試頁'將被顯示在瀏覽器中。

我的問題是爲什麼我必須在地址中指定端口8080?或換句話說,如何在不指定端口的情況下顯示我想要的地址http://localhost/

我知道我可以通過改變8080這裏的值更改端口

app.listen(8080); 
下面

基本應用:

var express = require('express'), 
    app = express(), 
    cons = require('consolidate'), 
    crypto = require('crypto'), 
    MongoClient = require('mongodb').MongoClient; 

app.engine('html', cons.swig); 
app.set('view engine', 'html'); 
app.set('views', __dirname + '/views'); 

MongoClient.connect('mongodb://localhost:27017/m101', function(err, db) { 
    if(err) throw err; 

    //set up a route to go to the page http://localhost:8080/ to see 'This is a test Page' 
    app.get('/', function(req, res){ 


     db.collection('hw1_3').findOne(function(err, doc) { 
      //do stuff here 

      return res.render('hello', { "name" : decrypted }); 
     }); 
    }); 

    //set up a route to go to the page http://localhost/test to see 'This is a test Page' 
    app.get('/test', function(req, res){ 
     return res.send('This is a test Page!!', 200); 
    }); 

    app.listen(8080); 
    console.log('Express server started on port 8080'); 
}); 

回答

0

http流量的默認端口爲80.如果綁定到80以外的任何端口,則需要在URL中指定端口。 app.listen(80)將照顧你的問題。

在Unixy系統上,需要根(管理員)訪問才能綁定到小於1024的任何端口,因此您必須運行服務器(如sudo node server.js)才能獲得端口80.您應綁定到更高端口(如8080 )在這種情況下,在你的機器上開發。

0

我很小白,但是我想說,localhost:8080顯示器代替www.somesite.com。我不會太在港口號碼中包裝。如果你將它部署到heroku或者你不會看到它的東西。

相關問題