【nodejs】nodejs啟動服務和讀取檔案
阿新 • • 發佈:2021-02-09
技術標籤:前端相關
話說學會了js,無所不能啊!後端語言像Java、c、c++等都不用學了!
1,啟動服務
//nodejs啟動服務 var http = require('http'); http.createServer(function(request,response){ response.writeHead(200,{'Content-Type':'text/plain'});//200是伺服器響應頭部 response.end('hello world!') }).listen(8888); console.log('在控制檯打印出....http://127.0.0.1:8888/')
右擊滑鼠-->run as js application-->ok,瀏覽器訪問http://127.0.0.1:8888/ 即可;
2,讀取檔案
//nodejs讀取檔案
const fs = require('fs');
try {
const data = fs.readFileSync('C:/Users/happy/Desktop/11.txt', 'utf-8');
// 等待操作結果返回,然後列印結果
console.log(data);
} catch(e) {
console.log('讀取檔案發生錯誤');
}
右擊滑鼠-->run as js application-->ok,控制檯輸出如下: