Node.js Local Web Development Guide

Run web applications like Express and Fastify on localhost using Node.js, common ports 3000 and 8080.


Node.js comes with a built-in HTTP module, which, when combined with frameworks like Express, Fastify, and Koa, allows for quick setup of local web services. Modern full-stack and API development is mostly done on localhost for debugging.

Common localhost Addresses

Framework/ToolDefault Address
Express / Generalhttp://localhost:3000
Next.jshttp://localhost:3000
Vite Development Serverhttp://localhost:5173
Create React Apphttp://localhost:3000

Ports can be customized in the code or environment variables, for example, process.env.PORT || 8080.

Minimal Express Example

const express = require('express');
const app = express();

app.get('/', (req, res) => {
  res.send('Hello from localhost');
});

app.listen(3000, () => {
  console.log('http://localhost:3000');
});

After running node app.js, visit http://localhost:3000.

Startup Methods

npm install
npm run dev      # Most projects
node server.js   # Direct run
npx vite         # Frontend dev server

Listening on All Interfaces

If you need access from a mobile device or local network during development, listen on 0.0.0.0:

app.listen(3000, '0.0.0.0');

You can still access it via http://localhost:3000.

Common Issues

EADDRINUSE Port Already in Use
Change the port or terminate the occupying process: lsof -i :3000 (macOS/Linux).

localhost Connection Refused
Ensure the service is running and check the firewall and listening address.

CORS Issues
When the frontend dev server (5173) requests the backend (3000), CORS must be configured on the backend or a proxy must be used.

Conclusion

Node.js local development typically uses ports like http://localhost:3000, eliminating the need for separate installations of Apache/Nginx, as the frameworks come with built-in development servers.

访客计数:------ Best viewed in Netscape Navigator · 800×600 © LocalHost Run