node.js createServer
2021-05-07 02:29
标签:url pat exec fun inf file case class data
node.js createServer 标签:url pat exec fun inf file case class data 原文地址:https://www.cnblogs.com/dissipate/p/14725534.htmlconst 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(`
404 not found
span
`)
}
}
console.log(‘=============‘)
})
server.listen(8080,()=>console.log(‘server.listen @_@‘))
文章标题:node.js createServer
文章链接:http://soscw.com/index.php/essay/83493.html