How can I load a HTML page using NodeJs?
Asked
Active
Viewed 5,480 times
3 Answers
5
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(''));
}
}

major-mann
- 2,602
- 22
- 37
1
It depends on what you are using to render the page. If you are using AngularJS for the front end, send your HTML as a string or with sendfile()
method of express middleware. Basically node and express are used to call rest API to get your HTML as a string and render with AngularJS.
e.g
var 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 the 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 attempt at sharing apps. Hope this helps. Currently, I am working on it when I have free time. So it's not complete but works fine.

Shakti Prakash Singh
- 2,414
- 5
- 35
- 59

Bharat Bhushan
- 2,077
- 2
- 21
- 34
-1
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')