Starting and Using the Dev Server
The Angular CLI dev server compiles your application in memory, serves it to the browser, and watches for file changes to trigger automatic rebuilds.
Starting the Server
ng serve
This compiles the app and serves it at http://localhost:4200. The first build takes a few seconds; subsequent rebuilds after file changes complete in under a second.
Common Flags
# Change the port
ng serve --port 4300
# Open the browser automatically
ng serve --open
# Bind to a different host (for network access)
g serve --host 0.0.0.0
# Disable live reload
ng serve --no-live-reload
# Serve with a specific configuration
ng serve --configuration development
What Happens When You Save a File
- The CLI detects the file change
- Webpack recompiles only the affected modules
- The new code is pushed to the browser via a WebSocket connection
- The browser updates the view without a full page reload
Stopping the Server
Press Ctrl+C in the terminal where the server is running.
Production vs Development Builds
The dev server uses the development configuration by default:
- Source maps enabled for debugging
- No minification
- Faster rebuild times
- Verbose error messages
For a production-like experience locally:
ng serve --configuration production
Proxying API Requests During Development
When your Angular app calls a backend API during development, you will hit CORS restrictions. The CLI provides a proxy configuration to forward API requests to your backend server.
Creating a Proxy Config
Create proxy.conf.json in your project root:
{
"/api": {
"target": "http://localhost:3000",
"secure": false,
"changeOrigin": true
}
}
Using the Proxy
ng serve --proxy-config proxy.conf.json
Or add it to angular.json so it is always used:
{
"architect": {
"serve": {
"options": {
"proxyConfig": "proxy.conf.json"
}
}
}
}
How It Works
When your app makes a request to /api/users, the dev server forwards it to http://localhost:3000/api/users. The browser sees a same-origin request, so CORS headers are not required.
Multiple Proxy Targets
{
"/api": {
"target": "http://localhost:3000",
"secure": false
},
"/auth": {
"target": "http://localhost:4000",
"secure": false
}
}
This lets you route different API prefixes to different backend services during development.
Quiz
1. What URL does ng serve use by default?
2. How do you change the dev server port?
3. What is the purpose of a proxy config in Angular development?
Flashcards
Question
How do you start the Angular dev server?
Click to reveal answer
Answer
ng serve — compiles in memory and serves at http://localhost:4200 with live reload.
Question
How do you proxy API requests in development?
Click to reveal answer
Answer
Create proxy.conf.json and run ng serve --proxy-config proxy.conf.json. Forwards /api to your backend.
Question
What does --open flag do?
Click to reveal answer
Answer
Automatically opens the browser when the dev server is ready.
Revision Notes
Key Takeaways
- 1. ng serve compiles in memory and serves at localhost:4200
- 2. Live reload rebuilds only changed files in under a second
- 3. Proxy config avoids CORS issues during development
- 4. Use --port, --open, and --host flags for common scenarios
Interview Tips
- • Explain how the dev server uses WebSocket for live reload
- • Be ready to describe the proxy pattern for API development
- • Mention that production builds use a different configuration
Cheat Sheet
ng serve Cheat Sheet
Start server:
ng serve
Common flags:
--port 4300— change port--open— open browser--host 0.0.0.0— network access--proxy-config proxy.conf.json— API proxy
Default URL: http://localhost:4200
Proxy config:
{
"/api": {
"target": "http://localhost:3000"
}
}