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 https://code.visualstudio.com/.

2.       Install Visual Studio Code.

3.       Download node,js https://nodejs.org/en/download/.

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 https://chanmingman.wordpress.com/2016/12/12/debug-c-using-visual-studio-code/.

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) {

         console.log(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

         response.write(data.toString());      

      }

      // Send the response body

      response.end();

   });  

}).listen(8081);

 

// Console will print the message

console.log(‘Server running at http://127.0.0.1:8081/’);

clip_image002

12.   Save the file as server.js.

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

14.   Paste the following code.

<html>

   <head>

      <title>Sample Page</title>

   </head>

   <body>

      Hello World!

   </body>

</html>

15.   Save the file as index.htm.

16.   Replace the line in launch.json.

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

to

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

17.   Click on Debug

18.   Click Launch Program.

clip_image004

                You will see in DEBUG CONSOLE the following message.

                “Server running at http://127.0.0.1:8081/

clip_image006

19.   Open Web Browser and type http://127.0.0.1:8081/index.htm.

You will see the Web Page showing.

clip_image008

Reference:

https://www.tutorialspoint.com/nodejs/nodejs_web_module.htm

Advertisements

About chanmingman

Since March 2011 Microsoft Live Spaces migrated to Wordpress (http://www.pcworld.com/article/206455/Microsoft_Live_Spaces_Moves_to_WordPress_An_FAQ.html) 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:

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s