Node中的http模块

195 阅读3分钟

什么是http模块

http 模块是Node.js官方提供的、用来创建web服务器的模块。通过http模块提供的http.creatServer() 方法,就能方便的把一台普通的电脑,变成一台服务器,从而对外提供Web资源。

导入http模块 require('http')

进一步立即http模块作用

image.png

IP地址

image.png

域名和域名服务器

image.png

注意:

  1. 单纯使用IP地址,互联网中的电脑也能够正常运行,单有了域名的加持,能让互联网世界变得更加方便。
  2. 在开发测试期间,127.0.0.1对应的域名是localhost,它们都代表我们自己的这台电脑,在使用效果上没有任何区别。

创建最基本的web服务器

image.png

image.png

image.png

image.png

创建服务器代码

//导入http模块
const http = require('http')
//创建web服务器
const server = http.createServer()
// 使用服务器实例的.on()方法,为服务器绑定一个request 事件
server.on('request', (req, res) => {
    //只要有客户端来请求我们自己的服务器,就会触发request事件,从而调用这个事件处理函数
    console.log('Someone visit our web server')

})
//启动服务器
//参数1为端口号
server.listen(80, () => {
    console.log('http server running at http://127.0.0.1')
})

req请求对象

  • req.url 是客户端请求的url地址
  • req.method 是客户端的method请求类型 image.png
const http = require('http')
const server = http.createServer()
server.on('request', (req, res) => {
    //req 是请求对象,它包含了与客户端相关的数据和属性,例如:
    //req.url 是客户端请求的URL地址
    //req.method 是客户端的method请求类型
    const str = `Your request url is ${req.url},and request method is ${req.method}`
    console.log(str)
})
server.listen(80, () => {
    console.log('http://127.0.0.1')
})

res响应对象

res.end()方法,向客户端响应一些内容 image.png

image.png

image.png

const http = require('http')
const server = http.createServer()
server.on('request', (req, res) => {
    //req 是请求对象,它包含了与客户端相关的数据和属性,例如:
    //req.url 是客户端请求的URL地址
    //req.method 是客户端的method请求类型
    const str = `Your request url is ${req.url},and request method is ${req.method}`
    console.log(str)
    //调用res.end()方法,向客户端响应一些内容
    res.end(str)
})
server.listen(80, () => {
    console.log('http://127.0.0.1')
})

解决中文乱码问题

res.setHeader('Content-Type','text/html;charset=utf-8')

image.png

const http = require('http')
const server = http.createServer()
server.on('request', (req, res) => {

    const str = `用户访问的url地址:${req.url},用户访问的post方法:${req.method}`
    console.log(str)
    //解决中文乱码问题 ,res.setHeader()  需要设置响应头Content-Type 的值为 text/html;charset=utf-8
    res.setHeader('Content-Type', 'text/html;charset=utf-8')
    //客户端响应的内容
    res.end(str)

})
server.listen(80, () => {
    console.log(`http://127.0.0.1`)
})

根据不同的url响应不同的html内容

const http = require('http')
const { url } = require('inspector')
const server = http.createServer()
server.on('request', (req, res) => {
    let content = `<h1>404 Not find!</h1>` //设置默认的内容为404 Not found
    if (req.url == '/' || req.url == '/index.html') {
        content = `<h1>首页</h1>` //3.用户请求的是首页
    } else if (req.url == '/about.html') {
        content = `<h1>关于页面</h1>` //4.用户请求的是关于页面
    }
    res.setHeader('Content-Type', 'text/html;charset=utf-8')//5.设置Content-Type响应头,防止乱码

    res.end(`${content}${req.url}`)//6.把内容发生给客户端
})
server.listen(80, () => {
    console.log(`server running at http://127.0.0.1`)
})

自己搭建服务器

image.png

//导入http模块
const http = require('http')
//导入fs模块
const fs = require('fs')
//导入path模块
const path = require('path')
//创建web服务器
const server = http.createServer()
// 监听web服务器的request事件
server.on('request', (req, res) => {
    //3.1获取到客户端请求的URL地址
    const url = req.url
    //3.2把请求的URL地址映射为具体文件的存放路径
    // let fpath = path.join(__dirname, url)
    //5.1预定义一个空白的文件存放路径
    let fpath = ''
    if (url === '/') {
        fpath = path.join(__dirname, './love/index.html')
    } else {
        fpath = path.join(__dirname, './love', url)
    }
    //4.1根据映射过来的文件路径读取文件
    fs.readFile(fpath, 'utf-8', (err, dataStr) => {
        //4.2读取文件失败后,向客户端响应固定的“错误信息”
        if (err) return res.end(`<h1>404 Not find</h1>`)
        res.end(dataStr)
    })
})
//启动服务器
server.listen(80, (err) => {
    console.log('Server listen at http://127.0.0.1')
})