Navigate
JavaScript Tutorial for Beginners
The Ultimate CSS Tutorial for Beginners
Data Structure Tutorial for Beginners
ReactJS Tutorial for Beginners
Java Tutorial for Beginners
Node.js Tutorial for Beginners
SQL Tutorial for Beginners
Content
- Creating a Basic Web Server
- Handling-Different-Routes
- Activity
- Quiz
- Updated on 10/09/2024
- 450 Views
Creating a Basic Web Server
Use the http module to create a web server that listens for requests and sends responses.
Handling Different Routes
Use the req.url property to handle different routes:
Activity
Build a simple web server with multiple routes and serve different HTML content based on the URL.
Quiz
Which module is used to create an HTTP server in Node.js?
- a) http
- b) fs
- c) os
- d) url
What does the res.statusCode property set?
- a) The response headers
- b) The status code of the HTTP response
- c) The URL of the request
- d) The response body
How do you handle different routes in an HTTP server?
- a) Use res.write() for each route
- b) Check req.url and respond accordingly
- c) Create separate servers for each route
- d) Use res.setHeader() for each route
What will be the response for an unknown route?
- a) 200 OK
- b) 500 Internal Server Error
- c) 404 Not Found
- d) 403 Forbidden
What does res.end() do in an HTTP server?
- a) Ends the HTTP request
- b) Ends the HTTP response and sends data to the client
- c) Closes the server
- d) Redirects to another URL