nodejs http server
const http = require("http");
const server = http.createServer((req, res) => {
console.log(req.url);
if (req.url === "/") {
res.write("<h1>This is home page</h1>");
} else if (req.url === "/about") {
res.write("<h1> This is about page </h1>");
} else if (req.url === "/contact") {
res.write("<h1>This is contact page</h1>");
} else {
res.write("<h1>Page not found</h1>");
}
res.end();
});
server.listen(3000, () => {
console.log("server is running");
});
const http = require('http');
const port = 3000;
console.log(`Server is running on port ${port}`);
http.createServer(function(req, res){
res.writeHead(200, {'Content-Type': 'text/html'});
res.write('Write some HTML here...');
res.end();
}).listen(port);
// save this as server.js
// you have to download node.js
// open terminal or command prompt
// type node server.js
// find your server at http://localhost:3000
// to end the server, go to that terminal and press ctrl+c
// code by VARSHITH REDDY SATTI
// to create a server in node.js you should.
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.write("write html code to display you test")
res.end();
}).listen(8080);
// save this as httpServer.js
// run this by typing node httpServer.js in the command line
// to acess your server got to http://localhost:8080
var http = require('http');
var fs = require('fs');
var path = require('path');
http.createServer(function (request, response) {
console.log('request ', request.url);
var filePath = '.' + request.url;
if (filePath == './') {
filePath = './index.html';
}
var extname = String(path.extname(filePath)).toLowerCase();
var mimeTypes = {
'.html': 'text/html',
'.js': 'text/javascript',
'.css': 'text/css',
'.json': 'application/json',
'.png': 'image/png',
'.jpg': 'image/jpg',
'.gif': 'image/gif',
'.svg': 'image/svg+xml',
'.wav': 'audio/wav',
'.mp4': 'video/mp4',
'.woff': 'application/font-woff',
'.ttf': 'application/font-ttf',
'.eot': 'application/vnd.ms-fontobject',
'.otf': 'application/font-otf',
'.wasm': 'application/wasm'
};
var contentType = mimeTypes[extname] || 'application/octet-stream';
fs.readFile(filePath, function(error, content) {
if (error) {
if(error.code == 'ENOENT') {
fs.readFile('./404.html', function(error, content) {
response.writeHead(404, { 'Content-Type': 'text/html' });
response.end(content, 'utf-8');
});
}
else {
response.writeHead(500);
response.end('Sorry, check with the site admin for error: '+error.code+' ..\n');
}
}
else {
response.writeHead(200, { 'Content-Type': contentType });
response.end(content, 'utf-8');
}
});
}).listen(8125);
console.log('Server running at http://127.0.0.1:8125/');