node.js重点模块
-
querystring
- 功能:是node.js中处理字符的
- 核心方法
- parse
var str = 'http://www.baidu.com:8080/001?a=1&b=2#hash=20' var obj = qs.parse( str,'?','&' )
- stringify
- escape
- unescape
- parse
-
http
核心方法
1.get
2.request
应用:小爬虫 -
events
-
fs
-
stream
Node.js原生路由
以下是原生路由的做法,通过监听前端发来的url,来作判断,弊端:图片需要重新发送出去
var http=require('http');
var fs=require('fs');
http.createServer((req,res)=>{
//console.log(req.url)
switch(req.url){
case '/hello':
res.write('hello')
res.end()
break;
case '/hehe':
res.write('hehe')
res.end()
break;
case '/my':
fs.readFile('./dist/1.txt',(error,data)=>{
if(error) throw error
res.write(data)
res.end()
})
break;
default:
break;
}
}).listen(8003,'localhost',()=>{
console.log('服务器运行在:http://localhost:8003')
})