xxxxxxxxxx
const http = require('http');
const fs = require('fs');
const path = require('path');
// console.log(http);
const server = http.createServer((req, res) => {
// console.log(req);
// console.log(req.url);
if (req.url === '/') {
// res.write('Welcome to Homepage');
// res.end();
fs.readFile(path.join(__dirname, 'index.html'), (err, data) => {
if (err) throw err;
res.writeHead(200, { 'Content-Type': 'text/html' });
res.write(data);
res.end();
});
} else {
if (req.url === '/user') {
res.write('Welcome to user page');
res.end();
}
}
});
server.listen(4000, () => {
console.log(`Server is up and running`);
});
xxxxxxxxxx
//HTTP MODULE NODE.JS
var http = require('http');
var server = http.createServer(function(req, res){
//write code here
});
server.listen(5000);
xxxxxxxxxx
var http = require('http');
//create a server object:
http.createServer(function (req, res) {
res.write('Hello World!'); //write a response to the client
res.end(); //end the response
}).listen(8080); //the server object listens on port 8080