node學習

建立一個小服務

var http = require("http");

function process_request(req,res) {
    var body = 'Thanks for calling!\n';
    var content_length = body.length;
    res.writeHead(200,{
        'Content-Length':content_length,
        'Content-Type':'text/plain'
    });
    res.end(body);
}

var s = http.createServer(process_request);
s.listen(8080)

http模塊容許建立服務器,能夠食用require來引入模塊
createServer 函數只會接受一個函數參數, 它會在用戶鏈接到服務器時被調用。node

屢次執行node index.js可能會報錯,緣由是在你監聽端口時這個線程會一直處於監聽狀態。npm

一句命令,中止某個端口
1.全局安裝服務器

npm install -g xl_close_port

2.關閉某一個端口 ( 8081 )函數

xl_close_port -p 8081
相關文章
相關標籤/搜索