nodejs模塊系統(tǒng)的操作nodejs中每一個(gè)函數(shù)都可以看作是一個(gè)模塊
nmp第三方組件
寫一個(gè)模塊:
- require引用另一個(gè)模塊
- console.log("./one"); 难裆,引入one,打印出來的是one里面的函數(shù)
- abc.zhouwu(); 打印的是one里面函數(shù)的結(jié)果
- module.exports =gaoming; 定義一個(gè)叫g(shù)aoming的模塊
-cc =new abc(); 類的實(shí)例化
- 可以傳參數(shù)
nodejs對文件的操作:看官方手冊中的程序
[]中的參數(shù)可要可不要饿凛,在所有編程類的手冊中的函數(shù)里
例子:fs模塊
var fs =require ('fs');
fs.readFile('./1.txt',(err, data) =>{
if (err) throw err;
console.log(data);
});
文件的讀取擂仍,fs的readFile方法讀取,如果有錯(cuò)誤,拋出異常
課下看一下:fs.readFile fs.realpath
var http = require('http');//引用http模塊
var ip = "192.168.238.129";//設(shè)置服務(wù)器的IP地址
var port = 3000;//端口,提供服務(wù)
http.createServer((req,res) =>{ //createServer是http里面的方法竟宋,req,用戶的請求物独,res服務(wù)器的響應(yīng)
res.writeHead('200',{'content-type':'text/html'});//設(shè)置文檔類型
res.write('<html>');//一下為向?yàn)g覽器輸出的內(nèi)容
res.write('<meta charset="utf-8">');
res.write('<body>');
res.write('<h1>我艸袜硫,今天星期五,明天不上班挡篓!');
res.write('</h1>');
res.write('</body>');
res.write('</html>');
res.end();//結(jié)束輸出
}).listen(port,ip,() =>{//監(jiān)聽,監(jiān)聽端口和IP地址
console.log('sever is run!');
});
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者