0%

What’s the Difference Between localhost and 127.0.0.1

What’s the Difference Between localhost and 127.0.0.1?

When front-end developers debug locally, they often interact with localhost, simply running npm run to open their webpage in a browser, with the address bar displaying something like http://localhost:xxx/index.html.

What is localhost?

localhost is a domain name, fundamentally no different from the domain names used for internet access, except for its ease of remembrance.

The scope of localhost is limited to the local machine

Both John Smith and Jane Doe can use localhost on their respective machines without interfering with each other, accessing their individual page content.

From Domain Name to Program

To truly understand localhost, we need to discuss how users access programs via domain names,

this article

localhost is a domain name

127.0.0.1? It’s an IP address, the local IP address of the current machine, which can only be used on the machine itself. our computer can use this IP address without being connected to the internet

How does localhost get resolved to 127.0.0.1 then? Does it go through DNS? No. Every computer can use localhost and 127.0.0.1 without DNS resolution.

This resolution is handled by each computer individually. There’s a hosts file on every computer with some hardcoded DNS resolution rules, including the rule for resolving localhost to 127.0.0.1, which is a convention.