2013-03-07 5 views
8

まずで急行アプリをテストするときに「エラーECONNREFUSEDを接続する」、私はポート1337のNode.jsは - ガイド:すべてのモカ

に聞いているポート80または8080でリッスンしていないよ私が作成しましたエクスプレスを使用して簡単なHTTPサーバー。サーバーを起動するには、app.jsスクリプトです。同じスクリプトでも、テストスクリプトで使用されますので、

require('./lib/server').listen(1337) 

サーバースクリプトは、lib/server.jsファイルに位置しています。

var http = require('http') 
, express = require('express') 
, app = express() 

app.get('/john', function(req, res){ 
    res.send('Hello John!') 
}) 

app.get('/sam', function(req, res){ 
    res.send('Hello Sam!') 
}) 

module.exports = http.createServer(app) 

そして、最後にtest/test.js

var server = require('../lib/server') 
, http = require('http') 
, assert = require('assert') 

describe('Hello world', function(){ 

    before(function(done){ 
     server.listen(1337).on('listening', done) 
    }) 

    after(function(){ 
     server.close() 
    }) 

    it('Status code of /john should be 200', function(done){ 
     http.get('/john', function(res){ 
      assert(200, res.statusCode) 
      done() 
     }) 
    }) 

    it('Status code of /sam should be 200', function(done){ 
     http.get('/sam', function(res){ 
      assert(200, res.statusCode) 
      done() 
     }) 
    }) 

    it('/xxx should not exists', function(done){ 
     http.get('/xxx', function(res){ 
      assert(404, res.statusCode) 
      done() 
     }) 
    }) 

}) 

しかし、私は3/3のエラーを取得:なぜこの私のテストスクリプトは思わ以来、私は本当に、理解していない

Hello world 
1) Status code of /john should be 200 
2) Status code of /sam should be 200 
3) /xxx should not exists 


✖ 3 of 3 tests failed: 

1) Hello world Status code of /john should be 200: 
Error: connect ECONNREFUSED 
    at errnoException (net.js:770:11) 
    at Object.afterConnect [as oncomplete] (net.js:761:19) 

2) Hello world Status code of /sam should be 200: 
Error: connect ECONNREFUSED 
    at errnoException (net.js:770:11) 
    at Object.afterConnect [as oncomplete] (net.js:761:19) 

3) Hello world /xxx should not exists: 
Error: connect ECONNREFUSED 
    at errnoException (net.js:770:11) 
    at Object.afterConnect [as oncomplete] (net.js:761:19) 

を論理。私はサーバーnode app.jsを走らせ、手作業でテストしました。localhost:1337/johnlocalhost:1337/samは素晴らしいです!

助けが必要ですか?ありがとう。

+0

は、ポート番号を変更しようとしましたか? – Amberlamps

+0

ポートは何であっても(何度もランダムに変更されていても)問題は同じです。 – htaidirt

+0

http.get()で 'http:// localhost:1337/john'を試しましたか? – Amberlamps

答えて

8

が機能するには、最初の引数としてのリソースへのフル・パスを割り当てる必要があります:

http.get('http://localhost:1337/john', function(res){ 
    assert(200, res.statusCode) 
    done() 
}) 
5
http.get({path: '/john', port: 1337}, function(res){ 
    //... 
}); 

他に何も指定されていない場合、http.getはポート80とみなされます。 http.get()については

+0

彼は彼がどのポートを使用していても動作しないと言った。私は彼が80でもそれを試したと仮定しています。 – Amberlamps

関連する問題