03.Node – Start With Example.
Node – Start With Example.
Steps,
- Make a folder – mkdir folderName (my folder name is nodeExample)
- Go to indide the folder – cd nodeExample
- Create a javaScript file inside nodeExample folder – example.js
- Copy and paste bellow code and save.
- Run js file (server). D:\OTHER\nodeExample\node example.js
1st Example
const http = require('http');
const hostname = '127.0.0.1';
const port = 3000;
const server = http.createServer((req, res) => {
//res.writeHead(200,{'Content-Type' : 'text/plain'});
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello World\n');
});
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
Node Module are the way to
import already build code I node.js.(like java package or class,library)
const http = require('http');
In here, require is similar to import in java, include in c++.
Anonymous Function, Billow
both are same.
var func = function(req,res){}
http.createServer(func);
http.createServer(function(req,res){});
.
Comments
Post a Comment