global對(duì)象(模塊)
window 和 global的區(qū)別
在瀏覽器上如果全局變量或者函數(shù),不屬于任何一個(gè)對(duì)象,那么就屬于window
在nodejs中,如果var聲明的變量不會(huì)屬于global對(duì)象,沒(méi)有用var聲明的全局變量是屬于global對(duì)象的。全局函數(shù)也不屬于global對(duì)象
var myname = 'enco';
myname2 = 'enco';
console.log(myname); // undefined
console.log(myname2); // enco
function fn(){
console.log('xixi');
}
global.fn(); // not a function
this
console.log(this); // {}
node函數(shù)中的this仍然指向的是調(diào)用者,如果一個(gè)函數(shù)沒(méi)有明確的調(diào)用者,就用global調(diào)用
function fn(){
console.log(this);
}
fn(); // 打印global
events 模塊
nodejs內(nèi)置模塊(非全局模塊,需要引入)
是nodejs的核心模塊,只有一個(gè)對(duì)象 eventEmitter
var events = require('events'); //引入模塊
var emitter = new events.EventEmitter(); // 創(chuàng)建EventEmitter對(duì)象實(shí)例
//設(shè)置時(shí)間的監(jiān)聽(tīng)
emitter.on('myevent',function(num1,num2,num3){
console.log(num1+num2+num3);
});
emitter.addListener('myevent',function(num1,num2,num3){
console.log(num1*num2*num3);
});
//只會(huì)觸發(fā)一次
emitter.once('myevent',function(num1,num2,num3){
console.log(num1*num2*num3);
});
//發(fā)射事件
emitter.emit('myevent',1,2,3);
移除事件監(jiān)聽(tīng)
emitter.removeListener('myevent',function(){});
注意:不能移除匿名監(jiān)聽(tīng)器
emitter.removeAllListeners(); 移除全部監(jiān)聽(tīng)器
http 模塊
搭建服務(wù)器
var http = require('http');
var server = http.createServer(function(request,response){
response.end('hello!');
})
server.listen(8080);
response
res.writeHeader();寫(xiě)入相應(yīng)頭信息
res.write();寫(xiě)入相應(yīng)體信息
res.end(); 結(jié)束本次請(qǐng)求
response.writeHeader(200,{
'Content-Type':'text/plain:charset=utf-8'
})
response.write("你好!不會(huì)亂碼");
response.end();
創(chuàng)建一個(gè)固定大小的Buffer對(duì)象的方法
var buffer = new Buffer.alloc(size, content,encoding);
buffer是個(gè)對(duì)象
request
url
var url = require("url");
var urlobj = url.parse(req.url);
url網(wǎng)站字符串的解析
url 模塊
url.parse 將整個(gè)url轉(zhuǎn)換成對(duì)象,url第二個(gè)參數(shù)填true,使參數(shù)部分變?yōu)閷?duì)象
url.format 功能相反
querystring 模塊
parse 將參數(shù)部分轉(zhuǎn)換為對(duì)象
stringify 功能相反
fs
fs.readFile(filePath[,options],callback);
post和get區(qū)別
post請(qǐng)求的參數(shù)會(huì)封裝在請(qǐng)求體,比較安全
get請(qǐng)求的參數(shù)會(huì)直接加在請(qǐng)求的url中
原生處理表單
if (req.method.toLowerCase() === 'post') {
res.writeHeader(200, {
'Content-Type': 'text/html;charset=utf-8'
})
var postbody = '';
req.on('data', function(chunk) {
postbody += chunk;
})
if (urlobj.pathname === '/login') {
req.on('end', function() {
var postobj = querystring.parse(postbody);
res.end('登錄成功!' + postobj.username + ':' + postobj.password);
})
} else if (urlobj.pathname === '/regist') {
req.on('end', function() {
var postobj = querystring.parse(postbody);
res.end('注冊(cè)成功!' + postobj.username + ':' + postobj.password + ':' + postobj.confirm-password);
})
}
fs.rename(oldpath,newpath,callback);
node.js 下的post
node認(rèn)為post請(qǐng)求過(guò)大,所以將請(qǐng)求體分塊通過(guò)異步的方法傳輸,使用事件監(jiān)聽(tīng)data和end事件
req.on(“data”,function(chunk){
})
req.on(“end”,function(chunk){
})
第三方模塊安裝
如果模塊需要被當(dāng)作全局命令來(lái)使用,就全局安裝;如果只是引入使用,就本地安裝。
最后編輯于 :2017.12.10 07:34:55
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者 【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。 平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。