美文网首页
NodeJS-url模块

NodeJS-url模块

作者: 走停2015_iOS开发 | 来源:发表于2018-07-02 11:44 被阅读8次
  • 导入模块创建服务
var http = require('http');
var url = require('url');
var server = http.createServer();
  • 处理response
server.on('request',function(req,res){

    //req.url :访问路径
    //console.log(req.url);///a/b/1.html
    //解析URL
   var  urlStr = url.parse(req.url);
    console.log(urlStr);
    //Url {
    //protocol: null,
    //    slashes: null,
    //    auth: null,
    //    host: null,
    //    port: null,
    //    hostname: null,
    //    hash: null,
    //    search: '?a=1',
    //    query: 'a=1',
    //    pathname: '/a/b/1.html',
    //    path: '/a/b/1.html?a=1',
    //    href: '/a/b/1.html?a=1' }
    switch (urlStr.pathname)
    {
        case '/':
            //首页
            res.writeHead(200,{
                'content-type':'text/html;charset=utf-8',
            })
            res.end('<h1>这是首页<h1>');
            break;
        case '/user':
            res.writeHead(200,{
                'content-type':'text/html;charset=utf-8',
            })
            res.end('<h1>个人中心<h1>');
            break;
        default :
            //处理其他的情况
            res.writeHead(404,{
                'content-type':'text/html;charset=utf-8',
            })
            res.end('<h1>页面出现错误<h1>');
            break;
            break;
    }

});
  • 监听
server.listen('8080','localhost');

相关文章

  • NodeJS-url模块

    导入模块创建服务 处理response 监听

  • nodejs-url

    KOA2: https://github.com/demopark/koa-docs-Zh-CN https://...

  • python常用模块!!

    os模块: stat模块: sys模块: hashlib,md5模块: random模块: types模块: at...

  • 2018-08-19

    Angular 2 技能图谱 模块 自定义模块 根模块 特性模块 共享模块 核心模块 内置模块 Applicati...

  • 【时间管理100讲】精髓全在这里啦

    理论模块 精力管理。 行动管理。 学习模块。 高空模块。 反思模块。 运动模块。 阅读模块。 旅行模块。 人际关系...

  • python基础学习(三)

    常用模块 String模块 数学模块 随机模块 OS模块 os.path模块 re模块 常用函数及操作 列表操作 ...

  • day10-异常处理和pygame显示

    一、异常处理 1.模块 导入模块(自定义模块,第三方模块)import 模块 ---->模块.内容from 模块 ...

  • 重点知识复习(异常处理)

    1.模块 导入模块(自定义模块,第三方模块,系统其他模块)import 模块 ----> 模块.内容from 模...

  • Python常用模块

    Python常用模块之time模块 Python常用模块之os模块 Python常用模块之sys模块 Python...

  • nodejs-模块

    nodejs模块 一、nodejs模块分类 1.核心模块 Core Module、内置模块、原生模块 fs模块 p...

网友评论

      本文标题:NodeJS-url模块

      本文链接:https://www.haomeiwen.com/subject/gvgkuftx.html