nodejs http模塊
阿新 • • 發佈:2018-05-05
scrip alt script color 最簡 簡單 esp 如圖所示 鏈式調用
創建一個最簡單的服務器
var http = require(‘http‘); var server = http.createServer(function (request, response) { response.end(‘ok‘); }); server.listen(8080);
啟動後在瀏覽器輸入http://127.0.0.1:8080 後如圖所示
我們可以再復雜點,調用fs模塊打開index.html,啟動瀏覽器就會去加載index.html頁面的內容
var http = require(‘http‘); var fs = require(‘fs‘); var server = http.createServer(function (request, response) { fs.readFile(‘index.html‘, ‘utf-8‘, function (err,data) { response.end(‘ok‘); }); }); server.listen(8080);
監聽代碼也可直接鏈式調用
var http = require(‘http‘); var fs = require(‘fs‘); var server = http.createServer(function (request, response) { fs.readFile(‘index.html‘, ‘utf-8‘, function (err,data) { response.end(‘ok‘); }); }).listen(8080);
nodejs http模塊