美文网首页
nodejs-http内置模块

nodejs-http内置模块

作者: 1CC4 | 来源:发表于2020-03-02 18:36 被阅读0次

安装http协议

npm i http -S

// 引入http协议
const http = require('http');

// 创建web服务实例
// 请求对象request,响应对象response
const webServer = http.createServer((request, response) => {
    // 1、接收请求
    let url = request.url;  //请求url
    // 跳过请求图标
    if(url == '/favicon.ico') return;
    console.log(url);
    // 2、处理业务 注册 登录
    response.writeHead(200, { 'Content-type': 'text/html;charset=utf-8' })
    //解决中文乱码
    if (url == '/login.do') {
        response.write('<h1>登录界面</h1>') //响应数据
    } else if (url == '/register.do') {
        response.write('<h1>注册界面</h1>')
    } else {
        response.write('hello word')
    }
    console.log(url);
    // 3、响应数据
    response.end(); //结束
});

启动web服务器


webServer.listen(8080, () => {
    console.log('web服务启动成功,监听8080端口')
})

运行

只有80端口才不写,其它都要加端口号http://ip:port/

相关文章

  • nodejs-http内置模块

    安装http协议 npm i http -S 启动web服务器 运行 只有80端口才不写,其它都要加端口号http...

  • NodeJS-http模块

    搭建一个http的服务器,用户处理用户发送的http请求,需要使用node提供的一个模块:http; res.wr...

  • nodejs-http模块

    1.http服务器http.createServer([options][, requestListener]) ...

  • node模块载入机制

    node内模块以及载入顺序为: 内置模块 文件模块 文件目录模块 node_modules模块 内置模块 http...

  • 四、node(二)

    node模块 文件模块内置模块第三方模块 内置模块 util util.inherits(Child,Parent...

  • Django自定义过滤器及标签

    自定义的引入:内置函数>>>>>>>>>>>>>>>自定义函数内置模块>>>>>>>>>>>>>>>自定义模块内置...

  • python基础篇07-日期模块

    python内置了多个用于日期和时间进行操作的内置模块:time模块,datetime模块和calendar模块。...

  • python入门 第十二天 模块

    python模块分为三种:1、 自定义模块 2、第三方模块 3、内置模块内置模块import sysfor i ...

  • Node.js HTTP模块

    内置HTTP模块 Node.js 中有 HTTP 这个内置模块,HTTP 模块允许 Node.js 通过超文本传输...

  • python常用模块介绍

    模块分为三种: 内置标准模块 自定义模块 开源模块(第三方) 以下主要介绍常用内置模块: 一、time模块 二、d...

网友评论

      本文标题:nodejs-http内置模块

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