博客
关于我
三、案例:留言板 & url.parse()
阅读量:360 次
发布时间:2019-03-05

本文共 2197 字,大约阅读时间需要 7 分钟。

url.parse()的使用与留言板案例分析

1. url.parse()的功能解析

url.parse() 是 Node.js 中用于解析 URL 的一个核心模块。该函数可以将 URL 分解为多个部分,包括 host、path、query 等。第二个参数 setUrlParsing 可以启用 URL 的解析功能,返回一个包含查询字符串的对象。这种方式非常适合处理带有 query 参数的 URL。

2. 留言板案例分析

2.1 HTML 模板与组件

留言板的 HTML 结构基于 Bootstrap 框架,主要包含以下组件:

  • header:包含页面标题和发表留言的按钮
  • comments:显示已有留言
  • post.html:用于发表留言的表单
2.2 表单提交逻辑

在留言板中,用户通过 post.html 表单提交留言信息。表单的 action 属性指定了提交 URL 为 /pinglun,method 为 get。提交时,表单控件需包含 name 属性,以便服务器端识别字段。

2.3 服务器端处理流程
  • 服务器接收请求,使用 url.parse() 解析 URL。
  • 根据 pathname 判断请求类型:
    • 如果为 /,返回 index.html 并渲染留言数据。
    • 如果为 /post,直接返回 post.html。
    • 如果为 /public,直接读取对应文件。
    • 如果为 /pinglun,处理留言提交逻辑:
      • 解析查询参数获取表单数据。
      • 添加当前时间戳,存储到留言数组中。
      • 重定向返回首页。
  • 2.4 错误页面处理

    未匹配的 URL 会返回 404.html 错误页面。

    3. app.js 代码解析

    const http = require('http')const fs = require('fs')const template = require('art-template')const url = require('url')const comments = [  { name: 'zep', message: '今天天气真不错', dateTime: '2021.3.23' },  // 其他留言...]const server = http.createServer((req, res) => {  const parseObj = url.parse(req.url, true)  const pathname = parseObj.pathname  if (pathname === '/') {    fs.readFile('./views/index.html', (err, data) => {      if (err) return res.end('404 Not Found.')      const htmlStr = template.render(data.toString(), { comments })      res.end(htmlStr)    })  } else if (pathname === '/post') {    fs.readFile('./views/post.html', (err, data) => {      if (err) return res.end('404 Not Found.')      res.end(data)    })  } else if (pathname.startsWith('/public')) {    fs.readFile('.' + pathname, (err, data) => {      if (err) return res.end('404 Not Found.')      res.end(data)    })  } else if (pathname === '/pinglun') {    const query = parseObj.query    const comment = { ...query, dateTime: '2021-03-22 23:10:15' }    comments.unshift(comment)    res.statusCode = 302    res.setHeader('Location', '/')    res.end()  } else {    fs.readFile('./views/404.html', (err, data) => {      if (err) return res.end('404 Not Found.')      res.end(data)    })  })}).listen(3000, () => {  console.log('Server run...')})

    4. 优化建议

    • 路径处理:建议使用 path 模块加密路径,避免路径Traversal 攻击。
    • 正则表达式:在解析 query 参数时,建议使用正则表达式过滤,避免 SQL 注入等安全问题。
    • 缓存机制:可以在客户端使用 localStorage 存储评论数据,减少服务器负载。
    • 错误处理:建议在每个读取操作中添加 try-catch 块,确保服务器稳定运行。

    转载地址:http://qhag.baihongyu.com/

    你可能感兴趣的文章
    vim杂谈(三)之配色方案
    查看>>
    vim杂谈(五)之vim不加载~/.vimrc
    查看>>
    Linux杂谈之终端快捷键
    查看>>
    vimscript学习笔记(二)预备知识
    查看>>
    vimscript学习笔记(三)信息打印
    查看>>
    awk杂谈之数组习题
    查看>>
    SSM项目中遇到Could not autowire. No beans of ‘XXX‘ type found.错误
    查看>>
    Linux网络属性配置详解
    查看>>
    Python(三十)类的理解
    查看>>
    Extjs布局详解
    查看>>
    Android数据库
    查看>>
    C语言之指针再涉(二)
    查看>>
    application类
    查看>>
    Linux基础命令(十四)软件安装的后续
    查看>>
    Perl(二)Perl简介
    查看>>
    HTML基础,块级元素/行内元素/行内块元素辨析【2分钟掌握】
    查看>>
    keil左侧文件调整方法
    查看>>
    本地分支关联远程分支
    查看>>
    函数求偏移量
    查看>>
    STM8 GPIO模式
    查看>>