11

私はnodeJSが初めてで、それを学ぼうとしています。http://net.tutsplus.com/tutorials/javascript-ajax/node-js-for-beginners/
から hello world の例を実行しようとして いますが、出力が得られず、クロムで No data received ページが表示されますブラウザ。 PC に apache (XAMPP) をインストールしましたが、アクティブではなく、ターミナルで実行しようとしても出力が得られません。 私は別のファイルhello.jsを持っています。これには 、実行時に端末に出力が表示されます。しかし、機能していません。 http.js コード:
node http.js
console.log('Hello World!');node hello.jsHello World!http.js

    // 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);
4

2 に答える 2

28

ノード 0.10.x 以降を使用していると思いますか? stream多くの場合、Streams2 と呼ばれる API にいくつかの変更が加えられています。Streams2 の新機能の 1 つはend、ストリームを完全に消費するまで (ストリームが空であっても)、イベントが発生しないことです。

実際にイベントでリクエストを送信したい場合はend、Streams 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);

または、ストリームを古い (流れる) モードに切り替えることができます。

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);

それ以外の場合は、すぐに応答を送信できます。

var http = require('http');

http.createServer(function (request, response) {

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

  response.end('Hello HTTP!');
}).listen(8080);
于 2013-10-26T15:49:26.760 に答える
5

これを試して

//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);
});
于 2016-09-24T18:34:48.277 に答える