HTTPモジュール
var http = require('http');
http.createServer(function (request, response) {
response.writeHead(200, {
'Content-Type': 'text/plain'});
response.end('Hello World');
}).listen(8081);
console.log('Server running at http://127.0.0.1:8081/');
-
var http = require('http');
インポートされたhttpモジュールを保存する変数を作成し、モジュールのインポートでrequireキーワードを使用します
-
http.createServer(function (request, response) { response.writeHead(200, { 'Content-Type': 'text/plain'}); response.end('Hello World'); }).listen(8081);
匿名関数である新しいサービスをhttpに追加します。パラメーター要求は、URLによって渡された情報を取得することであり、応答はブラウザーに応答することです。
response.writeHead(200, { 'Content-Type': 'text/plain'});
これは、応答ヘッダーを設定するためのものです
response.end('Hello World');
ページに文を出力して応答を終了することを意味します。応答を終了することを忘れないでください
-
console.log('Server running at http://127.0.0.1:8081/');
制御出力プロンプトでは、リスニングポートは8081です。
ES6書き込み
const http = require('http');
http.createServer((request, response)=>{
response.writeHead(200, {
'Content-Type': 'text/plain'});
response.end('Hello World');
}).listen(8080);
console.log('Server running at http://127.0.0.1:8080/');
URLモジュール
-
url.parse()
url.parse(urlStr, [parseQueryString], [slashesDenoteHost]);
受信パラメータ:
urlStrurl文字列
parseQueryStringがtrueの場合、クエリモジュールはクエリ文字列の分析に使用されます。デフォルトはfalseです。
slashesDenoteHost
デフォルトはfalseで、// foo / barの形式の文字列は{pathname: '// foo / bar'}として解釈されます。
trueに設定すると、// foo / barの形式の文字列は{host: 'foo'、pathname: '/ bar'}として解釈されます。
-
スーパーバイザー(ファイルの変更を監視し、サービスは自動的に再起動します)
npm install -g supervisor
指示
supervisor app.js