第一個node.js應用

前提:已經安裝好node.jsjavascript

新建文件helloworld.js,寫入代碼:html

var http = require('http');

http.createServer(function (request, response) {

	// 發送 HTTP 頭部 
	// HTTP 狀態值: 200 : OK
	// 內容類型: text/plain
	response.writeHead(200, {'Content-Type': 'text/plain'});

	// 發送響應數據 "Hello World"
	response.end('Hello World\n');
}).listen(8888);

// 終端打印以下信息
console.log('Server running at http://127.0.0.1:8888/');

在helloworld.js所在文件夾下按住「shift」+鼠標右鍵,選擇「在此處打開命令窗口」,打開cmd控制檯,輸入如下代碼:java

D:\NodeJs>node helloword.js

運行結果以下:node

在瀏覽器打開網址: http://127.0.0.1:8888/瀏覽器

 

參考:ui

  1. http://www.runoob.com/nodejs/nodejs-http-server.html
相關文章
相關標籤/搜索