Node.js原生路由

## Node.js原生路由
```javascript
以下是原生路由的做法,通过监听前端发来的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')
})
```

猜你喜欢

转载自www.cnblogs.com/zhaoyingzi/p/10872188.html