Create a HTTP server using Node.js and Express
This is a simple post which will create a web page containing a header and a paragraph. The basic motto of this post is to demonstrate the use of Express with Node.js . At the end of the post you will learn how to create static html pages using Node.js and express.
How To Run?
Screenshot
The Code
var express = require('express');
var app = express();
app.get('/', function (req, res) {
res.send('<div style="padding:100 100 100 100; text-align=center; width:650px;">'+
'<h1 style="border-bottom-width: 2px;border-bottom-style: solid;">'+
'This is a Sample Heading</h1><p>This is a sample Paragraph bellow the Header'+
'<br/>This is a simple Node Js and Express implementation.</p>'+
'<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. '+
'Nam in interdum felis. Duis rhoncus erat nunc, ut ultricies dolor fringilla'+
' vel. Donec ac suscipit mi. Sed risus lorem, fermentum non tempus sit amet,'+
' scelerisque sit amet nisi. Duis bibendum volutpat euismod. Duis efficitur,'+
' ligula quis tristique tempor, massa purus consectetur libero, a bibendum'+
' nulla quam vel mauris. Fusce auctor risus neque, non fringilla turpis congue in.'+
' Morbi malesuada elementum nisi id efficitur. Cum sociis natoque penatibus et'+
' magnis dis parturient montes, nascetur ridiculus mus. Maecenas bibendum ultricies'+
' molestie. Nam dapibus dui et ante volutpat, lacinia ullamcorper enim volutpat.</p>');
});
var server = app.listen(3000, function () {
var host = server.address().address;
var port = server.address().port;
console.log('Sample Server listening at http://%s:%s', host, port);
});
How To Run?
node filename.js
/*filename: replace it with you file.
In above code,
app.listen(3000, function () {..}
3000 is the port number
To access your webpage, open any web browser and type:
127.0.0.1:3000 or localhost:3000
*/
Got any doubts, do comment them here !

Comments
Post a Comment