Creating a Basic HTTP Route in Node.js
Description:โ
In this hands-on session, you will get your first taste of Node.js by creating a simple HTTP route. We'll explore the fundamentals of Node.js and how it handles HTTP requests. By the end of this exercise, you will be able to create a basic route that can send responses in both HTML and JSON formats.
Requirements:โ
- A computer with Node.js installed
- A code editor (e.g., Visual Studio Code)
- Basic understanding of JavaScript
Additional Challenges (Optional):โ
If you're looking for an extra challenge, try the following:
- Enhance your route to handle different HTTP methods like POST or GET.
- Implement error handling for invalid routes or requests.
- Create multiple routes and organize your code effectively.
Hints:โ
- Start by creating a Node.js script.
- Use the 'http' module to create an HTTP server.
- Define a route that listens for incoming requests.
- When a request is received, send an HTML response using the 'response' object.
- To send a JSON response, use the 'JSON.stringify' method.
Instructions:โ
- Open your code editor.
- Create a new JavaScript file (e.g.,
app.js
). - Import the 'http' module and create an HTTP server.
- Define a route (e.g., '/hello') that listens for incoming requests.
- When a request is made to your route, send an HTML response containing a greeting message.
- Additionally, modify your route to respond with a JSON object that includes a message.
- Test your route by opening a web browser and accessing 'http://localhost:PORT/route-name'.
- Verify that both HTML and JSON responses are displayed in the browser.
By completing this hands-on exercise, you'll gain a solid understanding of creating HTTP routes in Node.js and how to respond with different content types. This is a foundational skill for building web applications with Node.js. Good luck!
tip
app.js
const http = require('http');
const port = 3000; // You can choose any available port
const server = http.createServer((request, response) => {
if (request.url === '/') {
// Handle the root path
response.writeHead(200, { 'Content-Type': 'text/html' });
response.end('<h1>Hello, this is an HTML response</h1>');
} else if (request.url === '/json') {
// Handle a JSON route
const jsonData = { message: 'This is a JSON response' };
response.writeHead(200, { 'Content-Type': 'application/json' });
response.end(JSON.stringify(jsonData));
} else {
// Handle 404 Not Found
response.writeHead(404, { 'Content-Type': 'text/html' });
response.end('<h1>404 - Not Found</h1>');
}
});
server.listen(port, () => {
console.log(`Server is running on port ${port}`);
});