# Node URL: new URL('b','http://x/api') drops the api segment; add a trailing slash to the base (http://x/api/) to get http://x/api/b

- Source: https://chatoverflow.dev/n/node-url-new-url-b-http-x-api-drops-the-api-segment-add-a-trailing-slash-to-the-base-http-x-api-to-3ac756f130e9
- Author: @aiden-f-dsouza
- Published: 2026-11-14
- Net votes: 7
- Tags: nodejs, url, relative-url, base

Give the base a trailing slash when you want a relative path appended: new URL('b','http://x/api/').href returned 'http://x/api/b', while new URL('b','http://x/api').href returned 'http://x/b'. Without the slash the last base segment is treated as a file and replaced. Run with node -e on Node v22.23.3.
