get-request-pathname.js 518 B

1234567891011121314151617181920212223
  1. import {Errorf} from '@e22m4u/js-format';
  2. /**
  3. * Get request pathname.
  4. *
  5. * @param {import('http').IncomingMessage} req
  6. * @returns {string}
  7. */
  8. export function getRequestPathname(req) {
  9. if (
  10. !req ||
  11. typeof req !== 'object' ||
  12. Array.isArray(req) ||
  13. typeof req.url !== 'string'
  14. ) {
  15. throw new Errorf(
  16. 'The first argument of "getRequestPathname" should be ' +
  17. 'an instance of IncomingMessage, but %v given.',
  18. req,
  19. );
  20. }
  21. return (req.url || '/').replace(/\?.*$/, '');
  22. }