You're viewing old version number 9. - Current version

1 min

Learning Node.JS Programming

April 2014 - Better late than never, but I should have begun this process a few months ago.

Hello World at Command Prompt

Open up a text file and enter:

// Call the console.log function.
console.log("Hello World");

Save file as hello-console.js and then at the command prompt, execute:
node hello-console.js

Obviously, that produces:
Hello World

Hello World HTTP Server

Add the following code to a file called:
hello-server.js


// Load the http module to create an http server.
var http = require('http');

// Configure our HTTP server to respond with Hello World to all requests.
var server = http.createServer(function (request, response) {
  response.writeHead(200, {"Content-Type": "text/plain"});
  response.end("Hello World\n");
});

// Listen on port 8000, IP defaults to 127.0.0.1
server.listen(8000);

// Put a friendly message on the terminal
console.log("Server running at http://127.0.0.1:8000/");

Execute the code with:
node hello-server.js

The following message will be displayed to the console:
Server running at http://127.0.0.1:8000/

Point the browser to the domain/sub domain of the server with the port included. Example:
http://nodejs.soupmode.com:8000

If running the server and the browser on the same machine, use: http://localhost:8000

If the above server code is running, then the following will be displayed in the browser:
Hello World

To stop this server app, hit Ctrl-C at the command prompt.

#nodejs - #programming - #javascript - #howto

From JR's : articles
225 words - 2015 chars - 1 min read
created on
updated on - #
source - versions

Related articles
Learning Node.JS Programming - Nov 10, 2014
App idea to learn new code - Mar 10, 2014
Links feb 25, 2017 - Feb 25, 2017
React Native - Nov 01, 2016
NodeJS for newbies - Nov 01, 2013
more >>



A     A     A     A     A

© 2013-2017 JotHut - Online notebook

current date: Apr 30, 2024 - 3:07 a.m. EDT