User Tools

Site Tools


Sidebar

General

Python Tutorials

WebGL Tutorials

Node.js Tutorials

Node.js in WebStorm


Martin Christen

View Martin Christen's profile on LinkedIn


Twitter

Timeline of @MartinChristen

MartinChristen avatar

RT @snebiker: #PlayGameLieschtel - brilliant event by Kantonsbibliothek Baselland #KBL to get kids into libraries, including family #gamede…
About 2 days, 13 hours ago by: Martin Christen (@MartinChristen)

MartinChristen avatar

Only a handful tickets left for @PyConDE in Karlsruhe (October 24-28, 2018) - The schedule is now available at… https://t.co/Og4fp3DsoA
About 5 days, 6 hours ago by: Martin Christen (@MartinChristen)

MartinChristen avatar

IAMCP & @swissmade_sw Augmented Reality event just started https://t.co/bdREI6lJM4 #ar #AugmentedReality #Basel… https://t.co/9KpsdU3fvM
About 6 days, 12 hours ago by: Martin Christen (@MartinChristen)
node:tutorial01

Node.js Tutorials - Part 0

Node.js Tutorial 1

Installation

Download node.js for your platform at: http://nodejs.org/download/ (For Windows and MacOS you can download a simple installer.)

(Windows Users: Make sure Node is added to your PATH environment variable. This should be a default installation setting.)

Your First Application

The first thing we create is a simple “Hello World” application. Create a file hello.js with the following contents:

console.log('Hello World');

now open a console/terminal window and call

node hello.js

This will output “Hello World” in your console window. That's it! The first application is running.

A First Web Application

The next step is using Node.js to create a Web application. We create a server which creates the following JSON output:

{
   greeting: "Hello World"
}

First we need the http module. To access it use:

var http = require('http');

With the createServer of the http module a new server can be created. The syntax is: http.createServer([requestListener]). The requestListener is a function which is automatically added to the 'request' event. It returns a new web server object.

In our example we could create the server this way:

var myServer = http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'application/json'});
  res.end('{ greeting: "Hello World" }');
});

now we have to to listen on a certain port, this is done with the listen function:

myServer.listen(1234, '127.0.0.1')

After running this script you can point your webbrowser to “http://127.0.0.1:1234/” and you get the JSON answer.


node/tutorial01.txt · Last modified: 2013/09/16 22:04 by mchristen