How to load html page in Nodejs
or else can anybody give a sample code of loading html page in Nodejs
Thanks in Advance.
This code should load data from an html page.
var http = require('http');
http.get('http://www.google.com', onGotData);
function onGotData(res) {
var chunks = [];
res.on('data', onGotData);
res.on('end', onEnd);
function onGotData(chunk) {
chunks.push(chunk);
}
function onEnd() {
console.log(chunks.join(''));
}
}
User express. Follow this question
var express = require('express');
var app = express.createServer();
app.register('.html', require('jade'));
//app.engine for version 3
app.use(express.staticProvider(__dirname + '/public'));
//place your html in public folder beside the app.js
app.get('/', function(req, res) {
res.render('index.html');
});
// spin up server
app.listen(8080, '127.0.0.1')
its depend what you are using for rendering the page. if our are using using angularjs for front end. send your html as a string or with sendfile() method of express middle-ware. basically node and express is used for rest api give a call where your html is stored get as a string and render with angularjs.
e.g
ar express = require('express');
var app = express.createServer();
app.use(express.staticProvider(__dirname + '/public'));
app.all('/homePage', function(req, res) {
res.sendFile('home.html');
});
app.all('*', function(req, res) {
res.sendFile('index.html');
});
here index html is angular single page application with ng-view directive. render all the html at client side which you get from server.
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>Welcome</title>
<link rel="stylesheet" href="css/bootstrap-theme.css">
<link rel="stylesheet" href="css/style.css">
<link href="css/bootstrap.css" rel="stylesheet">
<script type="text/javascript" src="js/javascript.js"></script>
<script type="text/javascript" src="js/bootstrap.min.js"></script>
<script type="text/javascript" src="js/newangular.js"></script>
<script type="text/javascript" src="js/newroute1.js"></script>
<script type="text/javascript" src="js/ParentController.js"></script>
<script type="text/javascript" src="js/clintSIdeRoutes.js"></script>
<script type="text/javascript" src="js/services.js"></script>
</head>
<body ng-app="schoolApp" ng-controller="MainController">
<div class="container">
<div class="row"><span class="col-md-12"><h1>Header Goes here</h1></span></div>
<div ng-view></div><!-- this is place where your html is rendered -->
</div>
</body>
</html>
below is the link
https://github.com/bharat-daffodilsw/school
this is my first and staring apps. hope this helps currently i am working on it when i have free time. so its not completed but working fine.