Using node.js as a web server with Visual Studio Code

This blog shows you how to use node.js as web server. If you just want to test html, JavaScript, and CSS then you can use node.js as web server.

Do the following

1.       Download Visual Studio Code

2.       Install Visual Studio Code.

3.       Download node,js

4.       Install node.js.

5.       Run cmd (command promote) as Administrator.

6.       Create a folder for your Web Page.

7.       Change to the folder you have just created.

8.       Type Code . .

Visual Studio Code will be launched

9.       Configure the Visual Studio Code Debugger

10.   On Visual Studio Code click File, then choose New File.

11.   Paste the following code.

var http = require(‘http’);

var fs = require(‘fs’);

var url = require(‘url’);


// Create a server

http.createServer( function (request, response) { 

   // Parse the request containing file name

   var pathname = url.parse(request.url).pathname;


   // Print the name of the file for which request is made.

   console.log(“Request for ” + pathname + ” received.”);


   // Read the requested file content from file system

   fs.readFile(pathname.substr(1), function (err, data) {

      if (err) {


         // HTTP Status: 404 : NOT FOUND

         // Content Type: text/plain

         response.writeHead(404, {‘Content-Type’: ‘text/html’});

      }else {

         //Page found       

         // HTTP Status: 200 : OK

         // Content Type: text/plain

         response.writeHead(200, {‘Content-Type’: ‘text/html’});    


         // Write the content of the file to response body



      // Send the response body





// Console will print the message

console.log(‘Server running at’);


12.   Save the file as server.js.

13.   On Visual Studio Code click File, then choose New File.

14.   Paste the following code.



      <title>Sample Page</title>



      Hello World!



15.   Save the file as index.htm.

16.   Replace the line in launch.json.

“program”: “${workspaceRoot}/app.js”,


“program”: “${workspaceRoot}/server.js”,

17.   Click on Debug

18.   Click Launch Program.


                You will see in DEBUG CONSOLE the following message.

                “Server running at


19.   Open Web Browser and type

You will see the Web Page showing.



About chanmingman

Since March 2011 Microsoft Live Spaces migrated to Wordpress ( till now, I have is over 1 million viewers. This blog is about more than 50% telling you how to resolve error messages, especial for Microsoft products. The blog also has a lot of guidance teaching you how to get stated certain Microsoft technologies. The blog also uses as a help to keep my memory. The blog is never meant to give people consulting services or silver bullet solutions. It is a contribution to the community. Thanks for your support over the years. Ming Man is Microsoft MVP since year 2006. He is a software development manager for a multinational company. With 25 years of experience in the IT field, he has developed system using Clipper, COBOL, VB5, VB6, VB.NET, Java and C #. He has been using Visual Studio (.NET) since the Beta back in year 2000. He and the team have developed many projects using .NET platform such as SCM, and HR based applications. He is familiar with the N-Tier design of business application and is also an expert with database experience in MS SQL, Oracle and AS 400.
This entry was posted in .Net, Community, Computers and Internet, Java and tagged , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.