标签:url pat exec fun inf file case class data
const http=require(‘http‘)
const fs=require(‘fs‘)
const path=require(‘path‘)
const server=http.createServer(function(req,res){
const {url}=req
console.log(url,‘.‘+url)
const reg=/\w+\.(html|css|js)$/
const obj=reg.exec(url)
console.log(obj)
if(obj){
switch(obj[1]){
case ‘html‘:
fs.readFile(‘.‘+url,(err,data)=>{
if(err) return console.log(err)
res.end(data)
})
break
case ‘css‘:
fs.readFile(path.join(‘.‘,‘css‘,obj[0]),(err,data)=>{
if(err) return console.log(err)
res.end(data)
})
break
case ‘js‘:
fs.readFile(path.join(‘.‘,‘js‘,obj[0]),(err,data)=>{
if(err) return console.log(err)
res.end(data)
})
break
default:
res.end(`
<h1>404 not found</h1>
<span>span</span>
`)
}
}
console.log(‘=============‘)
})
server.listen(8080,()=>console.log(‘server.listen @_@‘))
标签:url pat exec fun inf file case class data
原文地址:https://www.cnblogs.com/dissipate/p/14725534.html