Skip to main content

Posts

Showing posts with the label http

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   ...

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 ele...