Skip to main content

Simple HTTP server programm in NodeJs

Most common use of Node is to create a servers. Node gives you very simple way to create different type of servers. Here using a Node i will create a simple HTTP server which will listen a port and response simple text message to every client which try to connect this HTTP server.

Create a httpserver.js:
var http = require('http');
var server = http.createServer();
server.on('request', function (req, res) {
    res.writeHead(200, {'Content-Type': 'text/plain'});
    res.end('Hello NodeJs Ninja\n');
});
server.listen(3000);
console.log('Server running at http://localhost:3000/');


Explanation:
Here what we have did. we have created a simple HTTP server which is listening port 3000 of your local server. Whenever a request happens, the anonymous[3] function (req, res) callback is fired and “Hello NodeJs Ninja” is written out as the response. You can compare it with the onclick event of the browser. Whenever user click on some element, event listener listen the call and trigger the function set to on click event. Here, Node provides a function that responds whenever a request happens.

Comments

Popular posts from this blog

Introduction to Node.js - Part-1 Node Command line Interface(CLI) App

Aim This document is get to start with the Node.Js. This is a simple introduction to Node and Its corresponding frameworks. This will help you to create a simple chit chat server application using node.js, express.js and socket.io. I have used the chat demo app of socket.io to learn you node. Introduction to Node.Js CLI Introduction to Node If you are from the Java and .net background. You might have create several JSP and ASP application to learn Java and ASP web framework. While create web app. You also learned JavaScript to interact with the HTML dom elements. If you go to the wikipedia definition of node which is “ Node.js is an open source, cross-platform runtime environment for server-side and networking applications. Node.js applications are written in JavaScript, and can be run within the Node.js runtime on OS X, Microsoft Windows, Linux, FreeBSD, and IBM_i ”. It tells that Node is a framework where you can write code in Javascript programming language on s

Loading- Exporting Modules in Node.Js

Loading a module: In node you can load module either using the path to the module or giving name  of the module. require keyword is used to load  module in node. Its look for the file in given path to load module if the require module is not core node module. You can install third party module using NPM (Node Package Manger). It downloads from the global repository to your local machine. var module = require( 'module-name' ); The require function returns an object that represent the module which have exported. It can be any javascript data type. It could be function, an array, any javascript object. Exporting a module: The CommonJs Module system is use to share the object or properties in node.js. Since Javaascript runs program in global scope. CommonJs Module is one of the best way for creating and sharing namespace in the javascript environment. Here below given an example to export a function in node.js. //person.js /** * Created by deepak.m.shrma@gm

Provide SSL support over your http NodeJs-Express API

A simple HTTPS server using node.js: Using ExpressJS and openssl, you can easily provide SSL support to your Api. For that you have to follow below given steps. Steps to create a https server. Generate an SSL certificate with that key Create a app.js file Install express module Run to Test your app. :-) Generate an SSL certificate with that key: For development purposes you can create a self-certified certificate. First, generate a private key on linux-based system. It will store a 1024 bit RSA key in the file key.pem openssl genrsa 1024 > key.pem Then, generate an SSL certificate with that key: openssl req -x509 -new -key key.pem > key-cert.pem Create a app.js file var fs = require("fs"),                 //Requires fs module to read key, cert files     express = require('express'),       // call express     app = express(),                     // define our app using express     http = require("http"),