Node.js is an open source server environment and run JavaScript on the server.
1. install Node.js - install from official site https://nodejs.org.
2. how to create and run file
step 1) make file with extension .js
step 2) save the file to other drive
step 3) open node.js command prompt
step 4) change directory in command eg: E: for e directory , D: for d directory
step 5) then subdirectory/folder = cd E:\nodejsfolder
step 6) type node filename.js
3. writing hello world using node.js
add below code to abc.js
var http = require("http");
http.createServer(function (request, response) {
response.writeHead(200, {'Content-Type': 'text/plain'});
response.end('Hello World\n');
}).listen(8081);
console.log('Server running at http://127.0.0.1:8081/');
open node.js command prompt(not normal command prompt) and type node abc.js
command prompt shows message "Server running at http://127.0.0.1:8081/"
go to this server from browser and check link http://127.0.0.1:8081/ , you will see "hello world"
if you close command prompt , server also close. to continuous on server add 'forget' module
4. install require packages using command.
npm install name.
for example - npm install ws --> to add websocket
remember to run use 'node filename.js' from command
4. create server
var http = require("http");
var server = http.createServer(function(){
console.log("Hello");
});
server.listen(3000);
now go to browser and open localhost:3000 & then check command prompt it show hello means server started.
please comment if any query.
No comments:
Post a Comment