模极社社区 - 制造业全产业链交流平台

    • 登录
    • 版块
    • 最新
    • 话题
    • 热门
    • 用户
    • 群组

    Encode URL——模具行业从业者必备知识手册

    社区公告
    1
    1
    1
    正在加载更多帖子
    • 从旧到新
    • 从新到旧
    • 最多赞同
    回复
    • 在新帖中回复
    登录后回复
    此主题已被删除。只有拥有主题管理权限的用户可以查看。
    • S
      system 2 最后由 编辑

      <h2>Encode URL</h2>

      <p>Encode a URL to a percent-encoded form, excluding already-encoded sequences.</p>

      <h3>Installation</h3>

      <pre><code>
      npm install encodeurl
      </code></pre>

      <h3>API</h3>

      <pre><code>
      var encodeUrl = require('encodeurl')
      </code></pre>

      <h4>encodeUrl(url)</h4>

      <p>Encode a URL to a percent-encoded form, excluding already-encoded sequences.</p>

      <p>This function accepts a URL and encodes all the non-URL code points (as UTF-8 byte sequences). It will not encode the "%" character unless it is not part of a valid sequence (<code>%20</code> will be left as-is, but <code>%foo</code> will be encoded as <code>%25foo</code>).</p>

      <p>This encode is meant to be "safe" and does not throw errors. It will try as hard as it can to properly encode the given URL, including replacing any raw, unpaired surrogate pairs with the Unicode replacement character prior to encoding.</p>

      <h3>Examples</h3>

      <h4>Encode a URL containing user-controlled data</h4>

      <pre><code>
      var encodeUrl = require('encodeurl')
      var escapeHtml = require('escape-html')

      http.createServer(function onRequest (req, res) {
      // get encoded form of inbound url
      var url = encodeUrl(req.url)

      // create html message
      var body = '<p>Location ' + escapeHtml(url) + ' not found</p>'

      // send a 404
      res.statusCode = 404
      res.setHeader('Content-Type', 'text/html; charset=UTF-8')
      res.setHeader('Content-Length', String(Buffer.byteLength(body, 'utf-8')))
      res.end(body, 'utf-8')
      })
      </code></pre>

      <h4>Encode a URL for use in a header field</h4>

      <pre><code>
      var encodeUrl = require('encodeurl')
      var escapeHtml = require('escape-html')
      var url = require('url')

      http.createServer(function onRequest (req, res) {
      // parse inbound url
      var href = url.parse(req)

      // set new host for redirect
      href.host = 'localhost'
      href.protocol = 'https:'
      href.slashes = true

      // create location header
      var location = encodeUrl(url.format(href))

      // create html message
      var body = '<p>Redirecting to new site: ' + escapeHtml(location) + '</p>'

      // send a 301
      res.statusCode = 301
      res.setHeader('Content-Type', 'text/html; charset=UTF-8')
      res.setHeader('Content-Length', String(Buffer.byteLength(body, 'utf-8')))
      res.setHeader('Location', location)
      res.end(body, 'utf-8')
      })
      </code></pre>

      <h3>Similarities</h3>

      <p>This function is similar to the intrinsic function <code>encodeURI</code>. However, it will not encode:</p>

      <ul>
      <li>The <code></code>, <code>^</code>, or <code>|</code> characters</li>
      <li>The <code>%</code> character when it's part of a valid sequence</li>
      <li><code>[</code> and <code>]</code> (for IPv6 hostnames)</li>
      <li>Replaces raw, unpaired surrogate pairs with the Unicode replacement character</li>
      </ul>

      <p>As a result, the encoding aligns closely with the behavior in the [WHATWG URL specification][whatwg-url]. However, this package only encodes strings and does not do any URL parsing or formatting.</p>

      <p>It is expected that any output from <code>new URL(url)</code> will not change when used with this package, as the output has already been encoded. Additionally, if we were to encode before <code>new URL(url)</code>, we do not expect the before and after encoded formats to be parsed any differently.</p>

      <h3>Testing</h3>

      <pre><code>
      $ npm test
      $ npm run lint
      </code></pre>

      <h3>References</h3>

      <ul>
      <li>[RFC 3986: Uniform Resource Identifier (URI): Generic Syntax][rfc-3986]</li>
      <li>[WHATWG URL Living Standard][whatwg-url]</li>
      </ul>

      <p>[rfc-3986]: https://tools.ietf.org/html/rfc3986</p>
      <p>[whatwg-url]: https://url.spec.whatwg.org/</p>

      <h3>License</h3>

      <p><a href="LICENSE">MIT</a></p>

      <hr>

      <p>本文由模极社发布</p>

      <hr>
      <p>!<a href="upload://cTUm8tHLABMewfCKi5BBEMzPd5E.svg">Encode URL</a></p>

      <hr>
      <h4>🔗 你可能还感兴趣</h4>
      <ul>
      <li><a href="/t/4482/4482">Contributing</a></li>
      <li><a href="/t/4302/4302">cookie</a></li>
      <li><a href="/t/3662/3662">深圳佳顺智能MIM输送带</a></li>
      </ul>

      <hr>
      <p><strong>相关推荐:</strong></p>
      <ul>
      <li><a href="https://www.mj7.cn/t/4836-4836">北京模具产业 — 科技研发驱动型模具基地</a></li>
      <li>[上海模具产业 — 高端精密模具研发中心]</li>
      </ul>

      <hr>
      <p>你们厂里是怎么处理的?来评论区聊聊。</p>

      1 条回复 最后回复 回复 引用 0
      • First post
        Last post
      Powered by NodeBB | Contributors