0% found this document useful (0 votes)
18 views6 pages

How To Create Server With Nodejs

The document discusses how to create an HTTP server in Node.js using the HTTP module. It explains that the HTTP module provides the ability to create servers and handle HTTP requests and responses. It then demonstrates creating a server that listens on port 3000, sets the content type and sends a "hello world" response string.

Uploaded by

sarveshsdeshmukh
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
0% found this document useful (0 votes)
18 views6 pages

How To Create Server With Nodejs

The document discusses how to create an HTTP server in Node.js using the HTTP module. It explains that the HTTP module provides the ability to create servers and handle HTTP requests and responses. It then demonstrates creating a server that listens on port 3000, sets the content type and sends a "hello world" response string.

Uploaded by

sarveshsdeshmukh
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 6

How to create

server in Nodejs

PW SKILLS
Content
1. HTTP module
2. Feature of HTTP module
3. Server with HTTP module

PW SKILLS
HTTP modules
● Hypertext Transport Protocol modules of Nodejs
● HTTP modules is the core modules of Nodejs
● HTTP modules provide way to create server and handle HTTP request and response.
● It exposes various classes, functions and properties to work with HTTP
● To use the HTTP server and Client one must require(“node:http”)

PW SKILLS
Features of HTTP method
● Create an HTTP server using `http.createServer()`
● Listen to incoming request using the `server.listen()` method
● Handle incoming requests and send responses using the `request` and `response` object passed to the server’s
request event
● Set headers on the response object using `response.setHeader()`
● Write data to the response object using `response.write()`
● End the response using `response.end()`
● Send an HTTP request to a server using `http.request()`
● Receive a response from a server using the `response` event of the `http.clientRequest` object returned by
`http.request()`

PW SKILLS
Server with HTTP module
const http = require("node:http"); // getting the http module

const port = 3000; // initializing the port no.


const host = "localhost"; // define the hostname

const server = http.createServer((req, res) => { // create server


res.statusCode = 200; // set to 200 to indicate it is successful
res.setHeader( "Content-type", "text/plain"); // set content type to plain text
res.end("hello world"); // end with ‘hello world’ text return
});

server.listen(port, () => { // list to the define port.


console.log(`Server running at ${host}:${port}`);
});

PW SKILLS
THANK
YOU
PW SKILLS

You might also like