Name: encodeurl
Owner: pillarjs
Description: Encode a URL to a percent-encoded form, excluding already-encoded sequences
Created: 2016-06-08 18:43:52.0
Updated: 2018-03-28 03:48:05.0
Pushed: 2018-01-22 03:12:23.0
Homepage: null
Size: 16
Language: JavaScript
GitHub Committers
User | Most Recent Commit | # Commits |
---|
Other Committers
User | Most Recent Commit | # Commits |
---|
Encode a URL to a percent-encoded form, excluding already-encoded sequences
This is a Node.js module available through the
npm registry. Installation is done using the
npm install
command:
m install encodeurl
encodeUrl = require('encodeurl')
Encode a URL to a percent-encoded form, excluding already-encoded sequences.
This function will take an already-encoded URL and encode all the non-URL
code points (as UTF-8 byte sequences). This function will not encode the
“%” character unless it is not part of a valid sequence (%20
will be
left as-is, but %foo
will be encoded as %25foo
).
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.
This function is similar to the intrinsic function encodeURI
, except it
will not encode the %
character if that is part of a valid sequence, will
not encode [
and ]
(for IPv6 hostnames) and will replace raw, unpaired
surrogate pairs with the Unicode replacement character (instead of throwing).
encodeUrl = require('encodeurl')
escapeHtml = require('escape-html')
.createServer(function onRequest (req, res) {
get encoded form of inbound url
r url = encodeUrl(req.url)
create html message
r body = '<p>Location ' + escapeHtml(url) + ' not found</p>'
send a 404
s.statusCode = 404
s.setHeader('Content-Type', 'text/html; charset=UTF-8')
s.setHeader('Content-Length', String(Buffer.byteLength(body, 'utf-8')))
s.end(body, 'utf-8')
encodeUrl = require('encodeurl')
escapeHtml = require('escape-html')
url = require('url')
.createServer(function onRequest (req, res) {
parse inbound url
r href = url.parse(req)
set new host for redirect
ef.host = 'localhost'
ef.protocol = 'https:'
ef.slashes = true
create location header
r location = encodeUrl(url.format(href))
create html message
r body = '<p>Redirecting to new site: ' + escapeHtml(location) + '</p>'
send a 301
s.statusCode = 301
s.setHeader('Content-Type', 'text/html; charset=UTF-8')
s.setHeader('Content-Length', String(Buffer.byteLength(body, 'utf-8')))
s.setHeader('Location', location)
s.end(body, 'utf-8')
m test
m run lint