因此呼叫出來設定即可
2.基本程式碼
hello_http.js
內容為
var http = require('http');
var ip = '127.0.0.1';
var port = 1337;
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(port, ip);
console.log('Server running at http://' + ip + ':' + port);
執行node hello_http.js
假設如上面程式碼所示只會監聽127.0.0.1的IP的請求
因此外面請求會無視
要讓外面可以請求
請將IP改成0.0.0.0
3.開啟網頁
輸入主機的IP和port有看到便是成功
4.停止執行
我們呼叫http的listen
它會一直監聽直到程式結束
因此需要按下Ctrl+C來強制結束

0 意見:
張貼留言