-
? ?node的優(yōu)勢
1、便于前端開發(fā)入門
2、性能高
3、利于前端代碼整合
查看全部 -
114查看全部
-
POST請求
(1)數(shù)據(jù)放在body中進(jìn)行傳輸(2)容量大:<2G(3)多段數(shù)據(jù)
思路:引入querystring模塊,創(chuàng)建數(shù)組獲取buffer多段數(shù)據(jù)并用concat拼接,querystring.parse(data)獲取json
let http = require('http')
let querystring = require('querystring') //引入querystring模塊
http.createServer((req, res) => {
? ? let result = []
? ? ? ? //Node.js中定義了Buffer類,專門用來存放二進(jìn)制數(shù)據(jù)的緩存區(qū)
? ? req.on('data', buffer => {
? ? ? ? result.push(buffer)
? ? })
? ? req.on('end', () => {
? ? ? ? //console.log(result);
? ? ? ? let data = Buffer.concat(result).toString();
? ? ? ? console.log(querystring.parse(data))
? ? })
}).listen(8080)
查看全部 -
GET請求
(1)獲取數(shù)據(jù);(2)數(shù)據(jù)放在url中進(jìn)行傳輸;(3)容量小:<32k
思路:url?user=xxx&password=xxx,引入url模塊,url.parse(req.url,true)獲取json
let http = require('http')
let url = require('url') //引入url模塊
http.createServer((req, res) => {
? ? let { pathname, query } = url.parse(req.url, true)
? ? console.log(pathname, query);
}).listen(8080)
查看全部 -
http模塊
服務(wù)器對象:http.createServer()
let?http?=?require('http')
let?fs?=?require('fs')
http.createServer((req,?res)?=>?{?//創(chuàng)建http服務(wù)
????fs.readFile(`./${req.url}`,?(err,?data)?=>?{?//讀取文件(路徑、回調(diào))
????????if?(err)?{
????????????res.writeHead(404)
????????????res.end('404?not?found')
????????}?else?{
????????????res.end(data)
????????}
????})
}).listen(8080)
查看全部 -
自定義模塊
一、require
1.如果有路徑就去路徑里面找
const mod1=require('./mod')
2.沒有的話就去node_modules那里面找
const mod1=require('mod')
3.再去node的安裝目錄里面找
二、exports和module
1.值
導(dǎo)出:exports.a=1;exports.b=2;let c=3;
使用:mod1.a? ??mod1.b? ??mod1.c
2.對象
導(dǎo)出:module.exports={a:1,b:2}
使用:mod1.a? ??mod1.b? ??mod1.c
3.函數(shù)
導(dǎo)出:module.exports=function(){}?
使用:mod1()
4.類
導(dǎo)出:module.exports=class{
? ?constructor(name){this.name=name}
? ?show(){console.log(this.name)}
}
使用:let? p=new mod1('myname');p.show()
查看全部 -
系統(tǒng)模塊:
1.path用于處理文件路徑和目錄路徑的實(shí)用工具
let path=require('path')
path.dirname? ? ?//路徑
path.basename? //文件名
path.extname? ? //擴(kuò)展名
path.resolve(__dirname,'index.js')? //絕對路徑
2.fs用于文件讀寫
let fs=require('fs')
//異步讀寫
fs.readFile('./a.text',(err,data)=>{})
fs.writeFile('./b.test','內(nèi)容',()=>{})
//同步讀寫
fs.readFileSync('a.text')
fs.writeFileSync('b.text','內(nèi)容')
查看全部 -
一.全局模塊(對象)
1.process.env? ?//打印環(huán)境變量
舉例:
if?(process.env.dev)?{console.log('我是開發(fā)環(huán)境')}?
else?{console.log('我是生產(chǎn)環(huán)境')}
2.process.argv? //返回一個(gè)數(shù)組
計(jì)算器舉例:
let?num1?=?parseInt(process.argv[2]);
let?num2?=?parseInt(process.argv[3]);
console.log(num1?+?num2);
//命令行輸入:node a 1 2,輸出3,計(jì)算器功能實(shí)現(xiàn)
二.系統(tǒng)模塊
三.自定義模塊
查看全部 -
npm init? ? ? ? ? ? //初始化
npm install xxx //安裝
npm i xxx? ? ? ? ? //安裝簡寫
npm uninstall xxx? //刪除
npm un xxxx? ? ? ? ? //刪除簡寫
npm install -g cnpm --registry=https://registry.npm.taobao.org
//通過淘寶鏡像安裝cnpm
npm update xxx //更新
npm install? ? //安裝依賴
查看全部 -
很好查看全部
-
kkk查看全部
-
bbn查看全部
-
12345
查看全部 -
環(huán)境變量? 1
查看全部 -
this a
查看全部
舉報(bào)