Questions And Answers

More Tutorials

NodeJS Tutorial

Hello World HTTP server

First, install Node.js for your platform.

In this example we'll create an HTTP server listening on port 1337, which sends Hello, World! to the browser. Note that, instead of using port 1337, you can use any port number of your choice which is currently not in use by any other service.

The http module is a Node.js core module (a module included in Node.js's source, that does not require installing additional resources). The http module provides the functionality to create an HTTP server using the http.createServer() method. To create the application, create a file containing the following JavaScript code.

const http = require('http'); // Loads the http module
http.createServer((request, response) => {
 // 1. Tell the browser everything is OK (Status code 200), and the data is in plain text
 response.writeHead(200, {
 'Content-Type': 'text/plain'
 // 2. Write the announced text to the body of the page
 response.write('Hello, World!\n');
 // 3. Tell the server that all of the response headers and body have been sent
}).listen(1337); // 4. Tells the server what port to be on

Save the file with any file name. In this case, if we name it hello.js we can run the application by going to the directory the file is in and using the following command:

node hello.js


In this page (written and validated by ) you learned about NodeJS Tutorial . What's Next? If you are interested in completing NodeJS tutorial, your next topic will be learning about: NodeJS npm.

Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue:

Share On:

Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using, you agree to have read and accepted our terms of use, cookies and privacy policy.