What is parseurl?
The parseurl npm package is used to parse URLs. It provides utilities for URL resolution and parsing to work with the components of URLs. It can be particularly useful in HTTP server handling to extract parts of the request URL.
What are parseurl's main functionalities?
Parse the URL of an HTTP request
This feature allows you to parse the URL of an incoming HTTP request and obtain components such as pathname, query, etc. The code sample creates an HTTP server that responds with the pathname of the request URL.
const parseurl = require('parseurl');
const http = require('http');
http.createServer(function (req, res) {
const parsedUrl = parseurl(req);
res.end('Pathname: ' + parsedUrl.pathname);
}).listen(3000);
Parse the same URL only once
This feature ensures that the URL is only parsed once and the result is cached. Subsequent calls to parseurl with the same request object will return the cached parsed URL object, improving performance.
const parseurl = require('parseurl');
const http = require('http');
http.createServer(function (req, res) {
const parsedUrl = parseurl(req);
// parseurl caches the parsed object in req._parsedUrl
// Subsequent calls will return the cached version
const sameParsedUrl = parseurl(req);
res.end('Pathname: ' + sameParsedUrl.pathname);
}).listen(3000);
Other packages similar to parseurl
url-parse
The url-parse package is similar to parseurl but offers more features, such as the ability to parse relative URLs and more detailed parsing of the query string. It can be used both in Node.js and in the browser.
qs
While qs is not a direct alternative to parseurl, it provides advanced query string parsing and stringifying capabilities. It can be used in combination with parseurl to handle complex query string scenarios.
parseurl
Parse a URL with memoization.
Install
$ npm install parseurl
API
var parseurl = require('parseurl')
parseurl(req)
Parse the URL of the given request object (looks at the req.url
property)
and return the result. The result is the same as url.parse
in Node.js core.
Calling this function multiple times on the same req
where req.url
does
not change will return a cached parsed object, rather than parsing again.
Benchmark
$ npm run-script bench
> parseurl@1.2.0 bench nodejs-parseurl
> node benchmark/index.js
> node benchmark/fullurl.js
Parsing URL "http://localhost:8888/foo/bar?user=tj&pet=fluffy"
1 test completed.
2 tests completed.
3 tests completed.
fasturl x 1,290,780 ops/sec ±0.46% (195 runs sampled)
nativeurl x 56,401 ops/sec ±0.22% (196 runs sampled)
parseurl x 55,231 ops/sec ±0.22% (194 runs sampled)
> node benchmark/pathquery.js
Parsing URL "/foo/bar?user=tj&pet=fluffy"
1 test completed.
2 tests completed.
3 tests completed.
fasturl x 1,986,668 ops/sec ±0.27% (190 runs sampled)
nativeurl x 98,740 ops/sec ±0.21% (195 runs sampled)
parseurl x 2,628,171 ops/sec ±0.36% (195 runs sampled)
> node benchmark/samerequest.js
Parsing URL "/foo/bar?user=tj&pet=fluffy" on same request object
1 test completed.
2 tests completed.
3 tests completed.
fasturl x 2,184,468 ops/sec ±0.40% (194 runs sampled)
nativeurl x 99,437 ops/sec ±0.71% (194 runs sampled)
parseurl x 10,498,005 ops/sec ±0.61% (186 runs sampled)
> node benchmark/simplepath.js
Parsing URL "/foo/bar"
1 test completed.
2 tests completed.
3 tests completed.
fasturl x 4,535,825 ops/sec ±0.27% (191 runs sampled)
nativeurl x 98,769 ops/sec ±0.54% (191 runs sampled)
parseurl x 4,164,865 ops/sec ±0.34% (192 runs sampled)
> node benchmark/slash.js
Parsing URL "/"
1 test completed.
2 tests completed.
3 tests completed.
fasturl x 4,908,405 ops/sec ±0.42% (191 runs sampled)
nativeurl x 100,945 ops/sec ±0.59% (188 runs sampled)
parseurl x 4,333,208 ops/sec ±0.27% (194 runs sampled)
License
MIT