const https = require('http')//引入系统模块HTTP
const app = https.createServer();//创建网站服务器
const url = require('url')

// 为网站服务器对象添加请求事件
app.on('request',(req,res) => {
    // 获取请求方式 为了判断方便并使用toLowerCase()方法 将返回的请求方式转换成小写
    const method = req.method.toLowerCase();
    // 获取不带参数的请求路径 即除去?之前的路径
    let {pathname} = url.prase(req.url).pathname;
    res.writeHead(200,{
        'content-type':'text/html;charset:utf8'
    })
    if(method == 'get'){
        if(pathname =='/' || pathname == '/index'){//当用户没有输入请求地址  默认为/
            res.end('欢迎来到首页')
        }
        else if(pathname == '/list'){
            res.end('欢迎来到某某叶')
        }
        else{
            res.end('抱歉 你走错片场了喔')
        }
    }
    else if(method == 'post'){

    }
    
});
app.listen(3000);
console.log('你的服务器启动成功');

 

posted on 2019-07-25 21:54  毛不易的小老婆  阅读(188)  评论(0编辑  收藏  举报