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

2021-05-20  本文已影响0人  過尽千帆_YL

1. 核心实现步骤

① 获取请求的 url 地址

② 设置默认的响应内容为 404 Not found

③ 判断用户请求的是否为 / 或 /index.html 首页

④ 判断用户请求的是否为 /about.html 关于页面

⑤ 设置 Content-Type 响应头,防止中文乱码

⑥ 使用 res.end() 把内容响应给客户端

2. 动态响应内容

const http = require('http')
const server = http.createServer()

server.on('request', (req, res) => {
  // 1. 获取请求的 url 地址
  const url = req.url
  // 2. 设置默认的响应内容为 404 Not found
  let content = '<h1>404 Not found!</h1>'
  // 3. 判断用户请求的是否为 / 或 /index.html 首页
  // 4. 判断用户请求的是否为 /about.html 关于页面
  if (url === '/' || url === '/index.html') {
    content = '<h1>首页</h1>'
  } else if (url === '/about.html') {
    content = '<h1>关于页面</h1>'
  }
  // 5. 设置 Content-Type 响应头,防止中文乱码
  res.setHeader('Content-Type', 'text/html; charset=utf-8')
  // 6. 使用 res.end() 把内容响应给客户端
  res.end(content)
})

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

案例 - 实现 clock 时钟的 web 服务器

1. 核心思路

把文件的实际存放路径,作为每个资源的请求 url 地址。

2. 实现步骤

① 导入需要的模块

② 创建基本的 web 服务器

③ 将资源的请求 url 地址映射为文件的存放路径

④ 读取文件内容并响应给客户端

⑤ 优化资源的请求路径

3. 步骤1 - 导入需要的模块

// 1.1 导入 http 模块
const http = require('http')
// 1.2 导入 fs 模块
const fs = require('fs')
// 1.3 导入 path 模块
const path = require('path')

3. 步骤2 - 创建基本的 web 服务器


// 2. 创建 web 服务器实例
const server = http.createServer()
// 3. 为服务器实例绑定 request 事件,监听客户端的请求
server.on('request', function (req, res) {
 
})
// 4. 启动服务器
server.listen(80, function () {  
  console.log('server running at http://127.0.0.1')
})

3. 步骤3 - 将资源的请求 url 地址映射为文件的存放路径

//3.1 获取到客户端请求的url地址
const url = req.url
//3.2把 请求的url地址,映射为本地文件的存放地址
const fpath = path.join(__dirname,url)

3. 步骤4 - 读取文件的内容并响应给客户端

  // 4.1 根据“映射”过来的文件路径读取文件的内容
  fs.readFile(fpath, 'utf8', (err, dataStr) => {
    // 4.2 读取失败,向客户端响应固定的“错误消息”
    if (err) return res.end('404 Not found.')
    // 4.3 读取成功,将读取成功的内容,响应给客户端
    res.end(dataStr)
  })

3. 步骤5 – 优化资源的请求路径

  let fpath = ''
  if (url === '/') {
    fpath = path.join(__dirname, './clock/index.html')
  } else {
    fpath = path.join(__dirname, '/clock', url)
  }

4. 完整实例

// 1.1 导入 http 模块
const http = require('http')
// 1.2 导入 fs 模块
const fs = require('fs')
// 1.3 导入 path 模块
const path = require('path')

// 2.1 创建 web 服务器
const server = http.createServer()
// 2.2 监听 web 服务器的 request 事件
server.on('request', (req, res) => {
  // 3.1 获取到客户端请求的 URL 地址
  //     /clock/index.html
  //     /clock/index.css
  //     /clock/index.js
  const url = req.url
  // 3.2 把请求的 URL 地址映射为具体文件的存放路径
  // const fpath = path.join(__dirname, url)
  // 5.1 预定义一个空白的文件存放路径
  let fpath = ''
  if (url === '/') {
    fpath = path.join(__dirname, './clock/index.html')
  } else {
    //     /index.html
    //     /index.css
    //     /index.js
    fpath = path.join(__dirname, '/clock', url)
  }

  // 4.1 根据“映射”过来的文件路径读取文件的内容
  fs.readFile(fpath, 'utf8', (err, dataStr) => {
    // 4.2 读取失败,向客户端响应固定的“错误消息”
    if (err) return res.end('404 Not found.')
    // 4.3 读取成功,将读取成功的内容,响应给客户端
    res.end(dataStr)
  })
})
// 2.3 启动服务器
server.listen(80, () => {
  console.log('server running at http://127.0.0.1')
})

上一篇 下一篇

猜你喜欢

热点阅读