YouTip LogoYouTip

Nodejs Http Server

# Node.js Create Your First Application In traditional PHP development, an HTTP server like Apache or Nginx is required, along with configuration of mod_php or php-cgi to process PHP scripts and generate dynamic content. This means PHP relies on an external HTTP server to receive requests and serve web pages. Node.js has a built-in HTTP server module. This means when developing with Node.js, developers can directly use Node.js's HTTP module to create servers, handle HTTP requests, and generate web pages. Therefore, in Node.js, the logic of web applications and the implementation of the HTTP server are tightly integrated, and developers do not need to rely on external HTTP server software. Before we create our first Node.js "Hello, World!" application, let's first understand what components make up a Node.js application: * **require directive**: In Node.js, the `require` directive is used to load and import modules. The imported module can be a built-in module, a third-party module, or a custom module. * **Create Server:** The server can listen for client requests, similar to HTTP servers like Apache or Nginx. * **Receive and Respond to Requests** Servers are easy to create. Clients can send HTTP requests using a browser or terminal. The server receives the request and returns a response. * * * ## Creating a Node.js Application ### Step 1: Use the require directive to load and import modules The syntax format is as follows: ```javascript const module = require('module-name'); Where `module-name` can be a file path (relative or absolute path) or a module name. If it is a module name, Node.js will automatically look for the module in the `node_modules` directory. The `require` directive returns the exported object of the loaded module. You can access the properties and methods defined in the module through this object. If the module has multiple exported objects, you can use destructuring assignment to obtain them. We use the **require** directive to load the `http` module and assign the instantiated HTTP to the variable `http`. Here is an example: ```javascript var http = require("http"); ### Step 2: Create the server Next, we use the `http.createServer()` method to create the server and use the `listen` method to bind to port 8888. The function receives and responds to data through the `request` and `response` parameters. Here is an example: create a file named `server.js` in the root directory of your project and write the following code: ## Example ```javascript var http = require('http'); http.createServer(function(request, response){ // Send HTTP header // HTTP Status: 200 : OK // Content-Type: text/plain response.writeHead(200,{'Content-Type':'text/plain'}); // Send response data "Hello World" response.end('Hello Worldn'); }).listen(8888); // Print the following information in the terminal console.log('Server running at http://127.0.0.1:8888/'); The above code completes a working HTTP server. Execute the above code using the **node** command: ```bash node server.js Server running at http://127.0.0.1:8888/ !(#) Next, open your browser and visit `http://127.0.0.1:8888/`. You will see a web page that says "Hello World". !(#) **Analysis of Node.js HTTP Server:** * `const http = require('http');`: Imports Node.js's built-in `http` module. * `http.createServer((req, res) => { ... });`: Creates a new HTTP server. The callback function is executed each time a request is received. * `res.writeHead(200, { 'Content-Type': 'text/plain' });`: Sets the response status code and content type. * `res.end('Hello Worldn');`: Ends the response and sends the data. * `server.listen(PORT, () => { ... });`: Listens on the specified port and outputs information after the server starts. * * * ## Gif Example Demonstration Next, we will demonstrate the example operation through a Gif image: !(#)
← Nodejs Module SystemNodejs Install Setup β†’