window+r快捷鍵彈出“命令行”輸入‘cmd’系統(tǒng)彈出命令提示符窗口
命令
1.切換盤符 d:
2.進入文件夾 cd 文件夾的名字
3.執(zhí)行某個文件 node 文件名
node的模塊系統(tǒng)
http 搭建后臺
/*--1.使用HTTP搭建服務(wù)器--*/
//1.引入http模塊
const http=require('http');
//2.創(chuàng)建服務(wù)
var server=http.createServer(function(req,res){
//console.log('服務(wù)器啟動了');
res.write('success');//響應(yīng)的內(nèi)容
res.end();//響應(yīng)結(jié)束
})
//3.指定端口號
server.listen(8080);
// node中的模塊系統(tǒng)
// 一。http搭建后臺服務(wù)器
/*--1.使用HTTP搭建服務(wù)器--*/
//1.引入http模塊
const http=require('http');
//2.創(chuàng)建服務(wù)
var server=http.createServer(function(req,res){
//req.url 請求的路徑
//res.write()//響應(yīng)的內(nèi)容
//res.end() 響應(yīng)結(jié)束
switch(req.url){
case'/1.html':
res.write("111");
break;
case'/2.html':
res.write("222");
break;
default:
res.write("404");
}
res.end();//響應(yīng)結(jié)束
})
//3.指定端口號
server.listen(8080);
FS文件操作模塊(讀取和寫文件)
讀取文件
const fs=require('fs');
//讀取文件
fs.readFile('aa.txt',function(err,data){//err錯誤 data數(shù)據(jù)
if(err){
console('cuo');
}else{
//console.log(data);
console.log(data.toString());
}
})
寫文件
//寫文件
var fs=require('fs');
//fs.writeFile('文件名','內(nèi)容',function(){})
fs.writeFile('bb.txt','zxc',function(err){
console.log(err);
})
fs模塊結(jié)合http模塊請求不同文件
// fs模塊結(jié)合http模塊請求不同文件
const http=require('http');
const fs=require('fs');
var server=http.createServer(function(req,res){
// req.url www
var file_name='./www'+req.url;
console.log(file_name);
fs.readFile(file_name,function(err,data){
if(err){
res.write('404');
}else{
res.write(data);
}
res.end();
})
});
server.listen(8080);
get方式
1.手動轉(zhuǎn)換
//http://localhost:8080/?uname=jack&upwd=123 獲取路徑 把數(shù)據(jù)轉(zhuǎn)換成對象格式{uname:jack,upwd:123}
const http=require('http');
var server=http.createServer(function(req,res){
// console.log(req.url);// /?uname=jack&upwd=123
//切割 split('切割符')
var GET={};
var arr=req.url.split('?');//['/','uname=jack&upwd=123']
var arr1=arr[1].split('&');//['uname=jack','upwd=123']
for(var i=0;i<arr1.length;i++){
var arr2=arr1[i].split('=');//['uname','jack'] [upwd,'123']
GET[arr2[0]]=arr2[1];//{uname:jack,upwd:123}
console.log(GET);
}
})
server.listen(8080);
2.querystring模塊
//querystring 模塊
const querystring=require('querystring');
var result=querystring.parse('uname=jack&upwd=123');
console.log(result);
url模塊
//URL方式
//1.http 引入模塊
const http=require('http');
//url
const url=require('url');
//2.創(chuàng)建服務(wù)
var server=http.createServer(function(req,res){
var obj=url.parse(req.url,true);
console.log(obj);
console.log(obj.pathname);
console.log(obj.query);
})
//3.端口號
server.listen(8080);
post
//get post
const http=require('http');
const querystring=require('querystring');
var server=http.createServer(function(req,res){
var str='';
req.on('data',function(data){//每次傳輸?shù)臄?shù)據(jù)
str+=data;
})
req.on('end',function(){//數(shù)據(jù)傳輸完成
var post=querystring.parse(str);
console.log(post);//uname=jack&upwd=123
})
});
server.listen(8080);