2013-10-26 19 views
9

Jestem nowy dla nodeJS i triyng, aby go poznać.
Próbuję wykonać przykład Witaj świecie z http://net.tutsplus.com/tutorials/javascript-ajax/node-js-for-beginners/ , ale nie otrzymuję żadnych danych wyjściowych i otrzymuję Brak strony otrzymanych danych w przeglądarce Chrome.
Mam zainstalowany apache (XAMPP) na moim komputerze, ale nie jest aktywny, a także gdy próbuję uruchomić node http.js w terminalu nie otrzymuję żadnych danych wyjściowych.
Mam jeszcze jeden plik hello.js, który zawiera console.log('Hello World!'); po uruchomieniu node hello.js otrzymuję wyjście Hello World! w terminalu. Ale http.js nie działa.
http.js Kod:http: // localhost: 8080/nie działa

// Include http module. 
var http = require("http"); 

// Create the server. Function passed as parameter is called on every request made. 
// request variable holds all request parameters 
// response variable allows you to do anything with response sent to the client. 
http.createServer(function (request, response) { 
    // Attach listener on end event. 
    // This event is called when client sent all data and is waiting for response. 
    request.on("end", function() { 
     // Write headers to the response. 
     // 200 is HTTP status code (this one means success) 
     // Second parameter holds header fields in object 
     // We are sending plain text, so Content-Type should be text/plain 
     response.writeHead(200, { 
     'Content-Type': 'text/plain' 
     }); 
     // Send data and end response. 
     response.end('Hello HTTP!'); 
    }); 
// Listen on the 8080 port. 
}).listen(8080); 
+1

Czy pojawiają się błędy w konsoli? Jakieś wskazówki co do tego, co się dzieje? Ten kod działa dobrze dla mnie, gdy idę do localhost: 8080 w przeglądarce. Czy próbowałeś wpisać: http://127.0.0.1:8080 w przeglądarce? Zarówno http://127.0.0.1:8080 i http: // localhost: 8080 działają dla mnie w mojej przeglądarce .. ale musisz podać port 8080 w url. – thtsigma

+0

używasz proxy? –

+0

Nie ma błędu w konsoli. Nie użyłem tutaj serwera proxy. –

Odpowiedz

26

Przypuszczam użyć węzła 0.10.x lub później? Ma pewne zmiany w interfejsie API stream, często określane jako strumienie2. Jedną z nowych funkcji w Streams2 jest to, że zdarzenie end nigdy nie jest uruchamiane, dopóki nie zużyjesz całego strumienia (nawet jeśli jest pusty).

Jeśli rzeczywiście chcesz wysłać prośbę o end razie można spożywać strumienia ze strumieniami 2 API:

var http = require('http'); 

http.createServer(function (request, response) { 

    request.on('readable', function() { 
     request.read(); // throw away the data 
    }); 

    request.on('end', function() { 

     response.writeHead(200, { 
     'Content-Type': 'text/plain' 
     }); 

     response.end('Hello HTTP!'); 
    }); 

}).listen(8080); 

lub można przełączyć strumień do trybu życia (płynący):

var http = require('http'); 

http.createServer(function (request, response) { 

    request.resume(); // or request.on('data', function() {}); 

    request.on('end', function() { 

     response.writeHead(200, { 
     'Content-Type': 'text/plain' 
     }); 

     response.end('Hello HTTP!'); 
    }); 

}).listen(8080); 

W przeciwnym razie, można wysłać odpowiedź od razu:

var http = require('http'); 

http.createServer(function (request, response) { 

    response.writeHead(200, { 
    'Content-Type': 'text/plain' 
    }); 

    response.end('Hello HTTP!'); 
}).listen(8080); 
2

Spróbuj

//Lets require/import the HTTP module 
var http = require('http'); 

//Lets define a port we want to listen to 
const PORT=8080; 

//We need a function which handles requests and send response 
function handleRequest(request, response){ 
    response.end('It Works!! Path Hit: ' + request.url); 
} 

//Create a server 
var server = http.createServer(handleRequest); 

//Lets start our server 
server.listen(PORT, function(){ 
    //Callback triggered when server is successfully listening. Hurray! 
    console.log("Server listening on: http://localhost:%s", PORT); 
}); 
Powiązane problemy