You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

120 lines
3.4 KiB

7 years ago
  1. # parseurl
  2. [![NPM Version][npm-image]][npm-url]
  3. [![NPM Downloads][downloads-image]][downloads-url]
  4. [![Node.js Version][node-version-image]][node-version-url]
  5. [![Build Status][travis-image]][travis-url]
  6. [![Test Coverage][coveralls-image]][coveralls-url]
  7. Parse a URL with memoization.
  8. ## Install
  9. ```bash
  10. $ npm install parseurl
  11. ```
  12. ## API
  13. ```js
  14. var parseurl = require('parseurl')
  15. ```
  16. ### parseurl(req)
  17. Parse the URL of the given request object (looks at the `req.url` property)
  18. and return the result. The result is the same as `url.parse` in Node.js core.
  19. Calling this function multiple times on the same `req` where `req.url` does
  20. not change will return a cached parsed object, rather than parsing again.
  21. ### parseurl.original(req)
  22. Parse the original URL of the given request object and return the result.
  23. This works by trying to parse `req.originalUrl` if it is a string, otherwise
  24. parses `req.url`. The result is the same as `url.parse` in Node.js core.
  25. Calling this function multiple times on the same `req` where `req.originalUrl`
  26. does not change will return a cached parsed object, rather than parsing again.
  27. ## Benchmark
  28. ```bash
  29. $ npm run-script bench
  30. > parseurl@1.3.1 bench nodejs-parseurl
  31. > node benchmark/index.js
  32. > node benchmark/fullurl.js
  33. Parsing URL "http://localhost:8888/foo/bar?user=tj&pet=fluffy"
  34. 1 test completed.
  35. 2 tests completed.
  36. 3 tests completed.
  37. fasturl x 1,290,780 ops/sec ±0.46% (195 runs sampled)
  38. nativeurl x 56,401 ops/sec ±0.22% (196 runs sampled)
  39. parseurl x 55,231 ops/sec ±0.22% (194 runs sampled)
  40. > node benchmark/pathquery.js
  41. Parsing URL "/foo/bar?user=tj&pet=fluffy"
  42. 1 test completed.
  43. 2 tests completed.
  44. 3 tests completed.
  45. fasturl x 1,986,668 ops/sec ±0.27% (190 runs sampled)
  46. nativeurl x 98,740 ops/sec ±0.21% (195 runs sampled)
  47. parseurl x 2,628,171 ops/sec ±0.36% (195 runs sampled)
  48. > node benchmark/samerequest.js
  49. Parsing URL "/foo/bar?user=tj&pet=fluffy" on same request object
  50. 1 test completed.
  51. 2 tests completed.
  52. 3 tests completed.
  53. fasturl x 2,184,468 ops/sec ±0.40% (194 runs sampled)
  54. nativeurl x 99,437 ops/sec ±0.71% (194 runs sampled)
  55. parseurl x 10,498,005 ops/sec ±0.61% (186 runs sampled)
  56. > node benchmark/simplepath.js
  57. Parsing URL "/foo/bar"
  58. 1 test completed.
  59. 2 tests completed.
  60. 3 tests completed.
  61. fasturl x 4,535,825 ops/sec ±0.27% (191 runs sampled)
  62. nativeurl x 98,769 ops/sec ±0.54% (191 runs sampled)
  63. parseurl x 4,164,865 ops/sec ±0.34% (192 runs sampled)
  64. > node benchmark/slash.js
  65. Parsing URL "/"
  66. 1 test completed.
  67. 2 tests completed.
  68. 3 tests completed.
  69. fasturl x 4,908,405 ops/sec ±0.42% (191 runs sampled)
  70. nativeurl x 100,945 ops/sec ±0.59% (188 runs sampled)
  71. parseurl x 4,333,208 ops/sec ±0.27% (194 runs sampled)
  72. ```
  73. ## License
  74. [MIT](LICENSE)
  75. [npm-image]: https://img.shields.io/npm/v/parseurl.svg
  76. [npm-url]: https://npmjs.org/package/parseurl
  77. [node-version-image]: https://img.shields.io/node/v/parseurl.svg
  78. [node-version-url]: http://nodejs.org/download/
  79. [travis-image]: https://img.shields.io/travis/pillarjs/parseurl/master.svg
  80. [travis-url]: https://travis-ci.org/pillarjs/parseurl
  81. [coveralls-image]: https://img.shields.io/coveralls/pillarjs/parseurl/master.svg
  82. [coveralls-url]: https://coveralls.io/r/pillarjs/parseurl?branch=master
  83. [downloads-image]: https://img.shields.io/npm/dm/parseurl.svg
  84. [downloads-url]: https://npmjs.org/package/parseurl