How to Get Started with Node.js

2014/05/312 min read
bookmark this
Responsive image

Table of Contents

  1. Create a Simple HTTP Server Using Node.js
  2. Additional Resources

Introduction

This blog is for people who want to get started learning Node.js by building a simple HTTP server.

Create a Simple HTTP Server Using Node.js

I'm a .NET developer, which means like other .NET developers, I use JavaScript as a front-end language and ASP.NET (MVC) with IIS as the server-side framework. Node.js was a big surprise to me, and I wanted to know how Node.js creates an HTTP server.

First, you need to download Node.js from the Node.js website.

Open the Node.js command prompt, go to a directory, and create a JavaScript file. Let's call it server.js, but any name is fine. The following is an example you can add to server.js. It will return "Hello World" in the browser.

var http = require('http');

http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World\n');
}).listen(1337, '127.0.0.1');

console.log('Server running at http://127.0.0.1:1337/');

After this, in the Node.js command window, navigate to the folder which contains server.js and type:

node server.js

Since the port is set to 1337, go to localhost:1337. You should be able to see "Hello World".

At this point, the simple HTTP Server with Node.js is finished.

Additional Resources

Following are a few links I found very interesting for going deeper into Node.js:

  1. StackOverflow - list of tutorials, videos, books, courses, blogs
  2. 7 Web Frameworks to help you build better Node.js apps
  3. Interesting article on building desktop apps with Node.js
  4. Huge list of modules you can use for Node.js
  5. From code to deploy to cloud service, very detailed documentation
  6. Beginner Web Framework: Express.js for Node.js
  7. Express.js example list

Conclusion

Creating a simple HTTP server with Node.js is straightforward and requires only a few lines of JavaScript. Node.js provides a lightweight and efficient way to build server-side applications, and the community offers plenty of resources and frameworks like Express.js to help you go further.