node.js createServer

  

 

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 @_@))

node.js createServer

 

 

node.js createServer

上一篇:Computer Networks COMPSCI 453: Select Lectures Notes (Resources and Foreword)


下一篇:HTTP缓存字段列表