Dino Geek, try to help you

How to use Node.js HTTP module?


Node.js HTTP module is used to build web server objects that listen to client’s requests similar to Apache HTTP Server. Here are the simple steps for using Node.js HTTP module:

Step 1: Include HTTP module
To include the HTTP module, use the require() method:
```
var http = require(‘http’);
```

Step 2: Create Server
Use the http.createServer() method to create an HTTP server:
```
var http = require(‘http’);

//create a server object:
http.createServer(function (req, res) { // Write a response to the client res.write(‘Hello World!’); //End the response res.end();
}).listen(8080); //the server object listens on port 8080
```
The function passed into the http.createServer() method, will be executed when someone tries to access the computer on port 8080.

Step 3: Save the file as myfirst.js, and initiate the file:
Go to the command line, enter the following:
```
node myfirst.js
```
Now, your computer works as a server. If anyone tries to access your computer on port 8080, they will get a “Hello World!” message in return.

Step 4: Access the localhost:8080 from your web browser
You can access localhost:8080 from your web browser, it will display “Hello World!”.

The HTTP module can create an HTTP server that listens to server ports and gives a response back to the client. When the server is ready, the callback function is called, in this case informing us that the server is running.


Simply generate articles to optimize your SEO
Simply generate articles to optimize your SEO





DinoGeek offers simple articles on complex technologies

Would you like to be quoted in this article? It's very simple, contact us at dino@eiki.fr

CSS | NodeJS | DNS | DMARC | MAPI | NNTP | htaccess | PHP | HTTPS | Drupal | WEB3 | LLM | Wordpress | TLD | Domain name | IMAP | TCP | NFT | MariaDB | FTP | Zigbee | NMAP | SNMP | SEO | E-Mail | LXC | HTTP | MangoDB | SFTP | RAG | SSH | HTML | ChatGPT API | OSPF | JavaScript | Docker | OpenVZ | ChatGPT | VPS | ZIMBRA | SPF | UDP | Joomla | IPV6 | BGP | Django | Reactjs | DKIM | VMWare | RSYNC | Python | TFTP | Webdav | FAAS | Apache | IPV4 | LDAP | POP3 | SMTP

| Whispers of love (API) | Déclaration d'Amour |






Legal Notice / General Conditions of Use