<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Encode URL——模具行业从业者必备知识手册]]></title><description><![CDATA[<p dir="auto">&lt;h2&gt;Encode URL&lt;/h2&gt;</p>
<p dir="auto">&lt;p&gt;Encode a URL to a percent-encoded form, excluding already-encoded sequences.&lt;/p&gt;</p>
<p dir="auto">&lt;h3&gt;Installation&lt;/h3&gt;</p>
<p dir="auto">&lt;pre&gt;&lt;code&gt;<br />
npm install encodeurl<br />
&lt;/code&gt;&lt;/pre&gt;</p>
<p dir="auto">&lt;h3&gt;API&lt;/h3&gt;</p>
<p dir="auto">&lt;pre&gt;&lt;code&gt;<br />
var encodeUrl = require('encodeurl')<br />
&lt;/code&gt;&lt;/pre&gt;</p>
<p dir="auto">&lt;h4&gt;encodeUrl(url)&lt;/h4&gt;</p>
<p dir="auto">&lt;p&gt;Encode a URL to a percent-encoded form, excluding already-encoded sequences.&lt;/p&gt;</p>
<p dir="auto">&lt;p&gt;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 (&lt;code&gt;%20&lt;/code&gt; will be left as-is, but &lt;code&gt;%foo&lt;/code&gt; will be encoded as &lt;code&gt;%25foo&lt;/code&gt;).&lt;/p&gt;</p>
<p dir="auto">&lt;p&gt;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.&lt;/p&gt;</p>
<p dir="auto">&lt;h3&gt;Examples&lt;/h3&gt;</p>
<p dir="auto">&lt;h4&gt;Encode a URL containing user-controlled data&lt;/h4&gt;</p>
<p dir="auto">&lt;pre&gt;&lt;code&gt;<br />
var encodeUrl = require('encodeurl')<br />
var escapeHtml = require('escape-html')</p>
<p dir="auto">http.createServer(function onRequest (req, res) {<br />
// get encoded form of inbound url<br />
var url = encodeUrl(req.url)</p>
<p dir="auto">// create html message<br />
var body = '&lt;p&gt;Location ' + escapeHtml(url) + ' not found&lt;/p&gt;'</p>
<p dir="auto">// send a 404<br />
res.statusCode = 404<br />
res.setHeader('Content-Type', 'text/html; charset=UTF-8')<br />
res.setHeader('Content-Length', String(Buffer.byteLength(body, 'utf-8')))<br />
res.end(body, 'utf-8')<br />
})<br />
&lt;/code&gt;&lt;/pre&gt;</p>
<p dir="auto">&lt;h4&gt;Encode a URL for use in a header field&lt;/h4&gt;</p>
<p dir="auto">&lt;pre&gt;&lt;code&gt;<br />
var encodeUrl = require('encodeurl')<br />
var escapeHtml = require('escape-html')<br />
var url = require('url')</p>
<p dir="auto">http.createServer(function onRequest (req, res) {<br />
// parse inbound url<br />
var href = url.parse(req)</p>
<p dir="auto">// set new host for redirect<br />
href.host = 'localhost'<br />
href.protocol = 'https:'<br />
href.slashes = true</p>
<p dir="auto">// create location header<br />
var location = encodeUrl(url.format(href))</p>
<p dir="auto">// create html message<br />
var body = '&lt;p&gt;Redirecting to new site: ' + escapeHtml(location) + '&lt;/p&gt;'</p>
<p dir="auto">// send a 301<br />
res.statusCode = 301<br />
res.setHeader('Content-Type', 'text/html; charset=UTF-8')<br />
res.setHeader('Content-Length', String(Buffer.byteLength(body, 'utf-8')))<br />
res.setHeader('Location', location)<br />
res.end(body, 'utf-8')<br />
})<br />
&lt;/code&gt;&lt;/pre&gt;</p>
<p dir="auto">&lt;h3&gt;Similarities&lt;/h3&gt;</p>
<p dir="auto">&lt;p&gt;This function is <em>similar</em> to the intrinsic function &lt;code&gt;encodeURI&lt;/code&gt;. However, it will not encode:&lt;/p&gt;</p>
<p dir="auto">&lt;ul&gt;<br />
&lt;li&gt;The &lt;code&gt;&lt;/code&gt;, &lt;code&gt;^&lt;/code&gt;, or &lt;code&gt;|&lt;/code&gt; characters&lt;/li&gt;<br />
&lt;li&gt;The &lt;code&gt;%&lt;/code&gt; character when it's part of a valid sequence&lt;/li&gt;<br />
&lt;li&gt;&lt;code&gt;[&lt;/code&gt; and &lt;code&gt;]&lt;/code&gt; (for IPv6 hostnames)&lt;/li&gt;<br />
&lt;li&gt;Replaces raw, unpaired surrogate pairs with the Unicode replacement character&lt;/li&gt;<br />
&lt;/ul&gt;</p>
<p dir="auto">&lt;p&gt;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.&lt;/p&gt;</p>
<p dir="auto">&lt;p&gt;It is expected that any output from &lt;code&gt;new URL(url)&lt;/code&gt; will not change when used with this package, as the output has already been encoded. Additionally, if we were to encode before &lt;code&gt;new URL(url)&lt;/code&gt;, we do not expect the before and after encoded formats to be parsed any differently.&lt;/p&gt;</p>
<p dir="auto">&lt;h3&gt;Testing&lt;/h3&gt;</p>
<p dir="auto">&lt;pre&gt;&lt;code&gt;<br />
$ npm test<br />
$ npm run lint<br />
&lt;/code&gt;&lt;/pre&gt;</p>
<p dir="auto">&lt;h3&gt;References&lt;/h3&gt;</p>
<p dir="auto">&lt;ul&gt;<br />
&lt;li&gt;[RFC 3986: Uniform Resource Identifier (URI): Generic Syntax][rfc-3986]&lt;/li&gt;<br />
&lt;li&gt;[WHATWG URL Living Standard][whatwg-url]&lt;/li&gt;<br />
&lt;/ul&gt;</p>
<p dir="auto">&lt;p&gt;[rfc-3986]: <a href="https://tools.ietf.org/html/rfc3986" rel="nofollow ugc">https://tools.ietf.org/html/rfc3986</a>&lt;/p&gt;<br />
&lt;p&gt;[whatwg-url]: <a href="https://url.spec.whatwg.org/" rel="nofollow ugc">https://url.spec.whatwg.org/</a>&lt;/p&gt;</p>
<p dir="auto">&lt;h3&gt;License&lt;/h3&gt;</p>
<p dir="auto">&lt;p&gt;&lt;a href="LICENSE"&gt;MIT&lt;/a&gt;&lt;/p&gt;</p>
<p dir="auto">&lt;hr&gt;</p>
<p dir="auto">&lt;p&gt;本文由模极社发布&lt;/p&gt;</p>
<p dir="auto">&lt;hr&gt;<br />
&lt;p&gt;!&lt;a href="upload://cTUm8tHLABMewfCKi5BBEMzPd5E.svg"&gt;Encode URL&lt;/a&gt;&lt;/p&gt;</p>
<p dir="auto">&lt;hr&gt;<br />
&lt;h4&gt;<img src="https://forum.mj7.cn/assets/plugins/nodebb-plugin-emoji/emoji/android/1f517.png?v=k0o2a4s5i30" class="not-responsive emoji emoji-android emoji--link" title=":link:" alt="🔗" /> 你可能还感兴趣&lt;/h4&gt;<br />
&lt;ul&gt;<br />
&lt;li&gt;&lt;a href="/t/4482/4482"&gt;Contributing&lt;/a&gt;&lt;/li&gt;<br />
&lt;li&gt;&lt;a href="/t/4302/4302"&gt;cookie&lt;/a&gt;&lt;/li&gt;<br />
&lt;li&gt;&lt;a href="/t/3662/3662"&gt;深圳佳顺智能MIM输送带&lt;/a&gt;&lt;/li&gt;<br />
&lt;/ul&gt;</p>
<p dir="auto">&lt;hr&gt;<br />
&lt;p&gt;&lt;strong&gt;相关推荐：&lt;/strong&gt;&lt;/p&gt;<br />
&lt;ul&gt;<br />
&lt;li&gt;&lt;a href="<a href="https://www.mj7.cn/t/4836-4836" rel="nofollow ugc">https://www.mj7.cn/t/4836-4836</a>"&gt;北京模具产业 — 科技研发驱动型模具基地&lt;/a&gt;&lt;/li&gt;<br />
&lt;li&gt;[上海模具产业 — 高端精密模具研发中心]&lt;/li&gt;<br />
&lt;/ul&gt;</p>
<p dir="auto">&lt;hr&gt;<br />
&lt;p&gt;你们厂里是怎么处理的？来评论区聊聊。&lt;/p&gt;</p>
]]></description><link>https://forum.mj7.cn/topic/7198/encode-url-模具行业从业者必备知识手册</link><generator>RSS for Node</generator><lastBuildDate>Fri, 02 Oct 2026 09:35:09 GMT</lastBuildDate><atom:link href="https://forum.mj7.cn/topic/7198.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 19 Apr 2026 21:53:23 GMT</pubDate><ttl>60</ttl></channel></rss>