美文网首页
node.js url.parse 的解析

node.js url.parse 的解析

作者: 梁同桌 | 来源:发表于2016-09-28 20:44 被阅读498次

文件服务器

让我们继续扩展一下上面的Web程序。我们可以设定一个目录,然后让Web程序变成一个文件服务器。要实现这一点,我们只需要解析request.url中的路径,然后在本地找到对应的文件,把文件内容发送出去就可以了。

解析URL需要用到Node.js提供的url模块,它使用起来非常简单,通过parse()将一个字符串解析为一个Url对象:

    'use strict';
     var url = require('url');
     console.log(url.parse('http://user:pass@host.com:8080/path/to/file?query=string#hash'));
  • 键值对
    • Url {
      protocol: 'http:',
      slashes: true,
      auth: 'user:pass',
      host: 'host.com:8080',
      port: '8080',
      hostname: 'host.com',
      hash: '#hash',
      search: '?query=string',
      query: 'query=string',
      pathname: '/path/to/file',
      path: '/path/to/file?query=string',
      href: 'http://user:pass@host.com:8080/path/to/file?query=string#hash' }

廖老师的
http://www.liaoxuefeng.com/wiki/001434446689867b27157e896e74d51a89c25cc8b43bdb3000/0014345015296018cac40c198b543fead5c549865b9bd4a000

个人博客: www.liangtongzhuo.com

相关文章

网友评论

      本文标题:node.js url.parse 的解析

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