# nodejs - hello world

Node.JS Hello World : tutorials.kode-blog.com 20160802

# Create package.json using npm

mkdir node
cd node
mkdir hello-world
cd hello-world
npm init
1
2
3
4
5

Fill in the answers to the utility questions.

# Node.JS Hello World Console Application

Create a new file app.js

Add the following code :

console.log('Hello World!');
1

Console is a global module that provides simple debugging console that is similar to the one JavaScript console in web browsers.

Run the following command :

node app.js
1

.js extension is not mandatory, this works also :

node app
1

# Node.JS Hello World Web Application

Create a new file web.js

Add the following code :

var http = require("http");

var port = 3000;

http.createServer(function(reqst, resp) {
    resp.writeHead(200, {'Content-Type': 'text/plain'});
    resp.end('Hello World!');
}).listen(port);

console.log('Load http://127.0.0.1:' + port + ' and watch the magic');
1
2
3
4
5
6
7
8
9
10

Run the following command :

node web
1

Load the following url in your browser : http://127.0.0.1:3000/