\href{https://npmjs.org/package/encodeurl}{\texttt{ }} \href{https://npmjs.org/package/encodeurl}{\texttt{ }} \href{https://nodejs.org/en/download}{\texttt{ }} \href{https://travis-ci.org/pillarjs/encodeurl}{\texttt{ }} \href{https://coveralls.io/r/pillarjs/encodeurl?branch=master}{\texttt{ }}

Encode a URL to a percent-\/encoded form, excluding already-\/encoded sequences\hypertarget{md_sfcode_node_modules_encodeurl_README_autotoc_md8393}{}\doxysection{Installation}\label{md_sfcode_node_modules_encodeurl_README_autotoc_md8393}
This is a \href{https://nodejs.org/en/}{\texttt{ Node.\+js}} module available through the \href{https://www.npmjs.com/}{\texttt{ npm registry}}. Installation is done using the \href{https://docs.npmjs.com/getting-started/installing-npm-packages-locally}{\texttt{ {\ttfamily npm install} command}}\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{\$ npm install encodeurl }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_encodeurl_README_autotoc_md8394}{}\doxysection{API}\label{md_sfcode_node_modules_encodeurl_README_autotoc_md8394}

\begin{DoxyCode}{0}
\DoxyCodeLine{var encodeUrl = require('encodeurl') }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_encodeurl_README_autotoc_md8395}{}\doxysubsection{encode\+Url(url)}\label{md_sfcode_node_modules_encodeurl_README_autotoc_md8395}
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 \char`\"{}\%\char`\"{} character unless it is not part of a valid sequence ({\ttfamily \%20} will be left as-\/is, but {\ttfamily foo} will be encoded as {\ttfamily \%25foo}).

This encode is meant to be \char`\"{}safe\char`\"{} 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 {\itshape similar} to the intrinsic function {\ttfamily encode\+URI}, except it will not encode the {\ttfamily \%} character if that is part of a valid sequence, will not encode {\ttfamily \mbox{[}} and {\ttfamily \mbox{]}} (for IPv6 hostnames) and will replace raw, unpaired surrogate pairs with the Unicode replacement character (instead of throwing).\hypertarget{md_sfcode_node_modules_encodeurl_README_autotoc_md8396}{}\doxysection{Examples}\label{md_sfcode_node_modules_encodeurl_README_autotoc_md8396}
\hypertarget{md_sfcode_node_modules_encodeurl_README_autotoc_md8397}{}\doxysubsection{Encode a URL containing user-\/controled data}\label{md_sfcode_node_modules_encodeurl_README_autotoc_md8397}

\begin{DoxyCode}{0}
\DoxyCodeLine{var encodeUrl = require('encodeurl')}
\DoxyCodeLine{var escapeHtml = require('escape-\/html')}
\DoxyCodeLine{}
\DoxyCodeLine{http.createServer(function onRequest (req, res) \{}
\DoxyCodeLine{  // get encoded form of inbound url}
\DoxyCodeLine{  var url = encodeUrl(req.url)}
\DoxyCodeLine{}
\DoxyCodeLine{  // create html message}
\DoxyCodeLine{  var body = '<p>Location ' + escapeHtml(url) + ' not found</p>'}
\DoxyCodeLine{}
\DoxyCodeLine{  // send a 404}
\DoxyCodeLine{  res.statusCode = 404}
\DoxyCodeLine{  res.setHeader('Content-\/Type', 'text/html; charset=UTF-\/8')}
\DoxyCodeLine{  res.setHeader('Content-\/Length', String(Buffer.byteLength(body, 'utf-\/8')))}
\DoxyCodeLine{  res.end(body, 'utf-\/8')}
\DoxyCodeLine{\}) }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_encodeurl_README_autotoc_md8398}{}\doxysubsection{Encode a URL for use in a header field}\label{md_sfcode_node_modules_encodeurl_README_autotoc_md8398}

\begin{DoxyCode}{0}
\DoxyCodeLine{var encodeUrl = require('encodeurl')}
\DoxyCodeLine{var escapeHtml = require('escape-\/html')}
\DoxyCodeLine{var url = require('url')}
\DoxyCodeLine{}
\DoxyCodeLine{http.createServer(function onRequest (req, res) \{}
\DoxyCodeLine{  // parse inbound url}
\DoxyCodeLine{  var href = url.parse(req)}
\DoxyCodeLine{}
\DoxyCodeLine{  // set new host for redirect}
\DoxyCodeLine{  href.host = 'localhost'}
\DoxyCodeLine{  href.protocol = 'https:'}
\DoxyCodeLine{  href.slashes = true}
\DoxyCodeLine{}
\DoxyCodeLine{  // create location header}
\DoxyCodeLine{  var location = encodeUrl(url.format(href))}
\DoxyCodeLine{}
\DoxyCodeLine{  // create html message}
\DoxyCodeLine{  var body = '<p>Redirecting to new site: ' + escapeHtml(location) + '</p>'}
\DoxyCodeLine{}
\DoxyCodeLine{  // send a 301}
\DoxyCodeLine{  res.statusCode = 301}
\DoxyCodeLine{  res.setHeader('Content-\/Type', 'text/html; charset=UTF-\/8')}
\DoxyCodeLine{  res.setHeader('Content-\/Length', String(Buffer.byteLength(body, 'utf-\/8')))}
\DoxyCodeLine{  res.setHeader('Location', location)}
\DoxyCodeLine{  res.end(body, 'utf-\/8')}
\DoxyCodeLine{\}) }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_encodeurl_README_autotoc_md8399}{}\doxysection{Testing}\label{md_sfcode_node_modules_encodeurl_README_autotoc_md8399}

\begin{DoxyCode}{0}
\DoxyCodeLine{\$ npm test}
\DoxyCodeLine{\$ npm run lint }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_encodeurl_README_autotoc_md8400}{}\doxysection{References}\label{md_sfcode_node_modules_encodeurl_README_autotoc_md8400}

\begin{DoxyItemize}
\item \href{https://tools.ietf.org/html/rfc3986}{\texttt{ RFC 3986\+: Uniform Resource Identifier (URI)\+: Generic Syntax}}
\item \href{https://url.spec.whatwg.org/}{\texttt{ WHATWG URL Living Standard}}
\end{DoxyItemize}\hypertarget{md_sfcode_node_modules_encodeurl_README_autotoc_md8401}{}\doxysection{License}\label{md_sfcode_node_modules_encodeurl_README_autotoc_md8401}
\mbox{[}MIT\mbox{]}(LICENSE) 