前端路由:根据路径不同,渲染不同的页面,一般情况下用作单页面,spa
后端路由:根据用户请求的路径或参数返回不同的页面或数据,一般用来定义接口
通过后端服务跳转不同路由,具体代码如下
const http = require('http')
const fs = require('fs')
http.createServer((req,res)=>{
if(req.url=='/'){
fs.readFile('./index.html',(err,data)=>{
if(!err){
res.writeHead('200',{'content-type':'text/html;charset=utf8'})
res.end(data)
}
})
}else if(req.url =='/list'){
fs.readFile('./list.html',(err,data)=>{
if(!err){
res.writeHead('200',{'content-type':'text/html;charset=utf8'})
res.end(data)
}
})
}else if(req.url=='/goods'){
fs.readFile('../http/data.json',(err,data)=>{
if(!err){
res.writeHead('200',{'content-type':'application/json;charset=utf8'})
res.end(data)
}
})
}
}).listen(9000)
注意 当在本地引入了jquery.js时要添加该路由 ,当页面引入了img图片时 ,也应写该图片路由如下截图。