社会Node.jsの(4)---のNode.jsは、httpサーバーを作成します

ステップ1 nodejs httpサーバを作成します。

  1. ロードモジュールはnodejsが必要です
  2. サーバーを作成し、クライアントをリッスン
  3. そして、要求に対する応答を受信

2 nodejs httpサーバのコードを作成します。

1.次のコードを記入し、server.jsという名前のファイルを作成します。

// 1. load http module
var http = require("http")
// 2. create http server
const server = http.createServer(function(request,response){
// send http head
// http status:     200:OK
// content type:     text/plain
response.writeHead(200,{'Content-Type': 'text/plain'});
// send response data
response.end('Hello World\n')
})
server.listen(8080)

2.新しいサーバーのhttpを実行します。 node server.js

3. [http://127.0.0.1:8080/にアクセスするためのブラウザ、あなたは「Hello World」のページを参照してくださいと言っています。

ます。https://www.jianshu.com/p/8bbdd529e1e4で再現

おすすめ

転載: blog.csdn.net/weixin_34138521/article/details/91184518