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:
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
Post a Comment