Thursday, June 12, 2014

Node.js load external html file

Example to load html code from external file, instead of code in js.


server.js
var http = require("http");
var fs = require("fs");

http.createServer(function(request, response){
    
    fs.readFile("page.html", function (err, data){
        if (err) {
            throw err; 
        }
        response.writeHead(200, {"Content-Type": "text/html"});
        response.write(data);
        response.end();
    });
    
}).listen(8888);

page.html
<html>
<head></head>
<body>
<h1>Hello World!</h1>
<p>from mobile-web-app.blogspot.com</p>
</body>
</html>


No comments:

Post a Comment