nodeJs学习-01 http模块

http模块基础:

const http = require("http"); //引入http系统模块

var server = http.createServer(function(req,res){       //创建一个服务,每当有访问的时候就会执行函数
    //request:      请求   输入-请求的信息
    //response:     响应    输出

    console.log(req.url);   //获取请求URL:index.html  favicon.icon网站图标
    switch(req.url){
        case '/1.html':
            res.write('request:'+'aa1111');
            break;
        case '/2.html':
            res.write('request:'+'bb2222');
            break;
        default:
            res.write('request:'+'404');
            break;
    }

    // res.write('abc');   //abc
    res.end();  //结束请求

    // console.log('有人来了');
});

//监听-某端口
server.listen(8004);   //端口:数字:比如123
    //在浏览器访问localhost:8080测试访问,就可触发函数
原文地址:https://www.cnblogs.com/LChenglong/p/11571064.html