Introduction to Node.js
A comprehensive guide to setting up and running a simple Node.js server with HTTP module
Understand the Problem
Problem Statement
What is Node.js?
Node.js is a powerful, open-source runtime environment that lets you run JavaScript on the server side. It features a non-blocking, event-driven architecture, ensuring high performance and scalability. This makes Node.js an excellent choice for modern web development, especially for real-time applications like chat systems and collaborative tools.
In this guide, we’ll introduce Node.js, highlight its core features and advantages, and provide a step-by-step tutorial to help you get started. Whether you’re new to backend development or looking to enhance your skills, this guide will help you understand why Node.js is essential in today’s web development landscape.
Setting Up Your Environment
To start with Node.js, you’ll need to install Node.js and npm (Node Package Manager). Follow these steps:
Constraints
- Node.js version 14.x or higher is required
- The server must listen on port 3000
- The server must respond with HTTP status code 200
- The response content type must be 'text/plain'
- The server should display 'Hello, World!' as the response body
Examples
No input required - server runs on startupServer running at http://127.0.0.1:3000/When you run 'node app.js' in terminal, the server starts and displays this message indicating successful initialization
HTTP GET request to http://127.0.0.1:3000/Hello, World!When you access the server URL in browser, it responds with 'Hello, World!' as plain text content
Solution
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define PORT 3000
#define BUFFER_SIZE 1024
int main() {
int server_fd, new_socket;
struct sockaddr_in address;
int addrlen = sizeof(address);
char buffer[BUFFER_SIZE] = {0};
char *response =
"HTTP/1.1 200 OK\r\n"
"Content-Type: text/plain\r\n"
"Content-Length: 13\r\n"
"\r\n"
"Hello, World!";
// Create socket file descriptor
if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0) {
perror("socket failed");
exit(EXIT_FAILURE);
}
// Set socket options
int opt = 1;
if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt, sizeof(opt))) {
perror("setsockopt");
exit(EXIT_FAILURE);
}
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons(PORT);
// Bind the socket to the port
if (bind(server_fd, (struct sockaddr *)&address, sizeof(address)) < 0) {
perror("bind failed");
exit(EXIT_FAILURE);
}
// Listen for incoming connections
if (listen(server_fd, 3) < 0) {
perror("listen");
exit(EXIT_FAILURE);
}
printf("Server running at http://%s:%d/\n", inet_ntoa(address.sin_addr), PORT);
while (1) {
// Accept incoming connection
if ((new_socket = accept(server_fd, (struct sockaddr *)&address, (socklen_t*)&addrlen)) < 0) {
perror("accept");
exit(EXIT_FAILURE);
}
// Read request
read(new_socket, buffer, BUFFER_SIZE);
// Send response
send(new_socket, response, strlen(response), 0);
printf("Hello message sent\n");
close(new_socket);
}
return 0;
}This C implementation creates a basic HTTP server using socket programming:
- Creates a socket using socket() system call
- Configures socket options for reuse
- Binds to port 3000 on all available interfaces
- Enters an infinite loop to accept incoming connections
- For each connection, reads the HTTP request and sends back a proper HTTP response
- The response includes proper HTTP headers with content type and length
Note: This is a simplified implementation for educational purposes. Production servers would need more robust error handling and request parsing.