0

I am using Raspberry Pi 3 with Windows IoT 10.I want to open a html webpage in the web browser.Here raspberry pi as web server,it has to display html page. I have tried in node js,but i am unable to read the html file. Here is my code

var http = require('http');
var fs = require('fs');
var path = require('path');
var filepath;
filepath = path.join(__dirname,'/assets/index.html');
console.log(filepath);
http.createServer(function (req, res) {
  //  console.log(filepath);
    fs.readFile(filepath, function (err, data) {
        if (error) return console.error("Error in finding the file");
        res.writeHead(200, { 'Content-Type': 'text/html', 'Content-Length': data.length });
        res.write(data);
    });

}).listen(1337);

I have create an index.html page under assets folder. Is this the correct way to host a web page or please please help me.

jpRam
  • 1
  • 1
    what happens in the browser when you try to "display html page" - hint: browser developer tools console - also, what URL do you use - uncomment the console.log line ... does it get output in the pi? – Jaromanda X Apr 19 '17 at 13:03
  • 1
    Possible duplicate of [Loading basic HTML in Node.js](http://stackoverflow.com/questions/4720343/loading-basic-html-in-node-js) – rymdmaskin Apr 19 '17 at 13:03
  • 3
    In line number 10 , you should writer `if (err) return console.error("Error in finding the file");` check for `err` instead of `error` – Ezzat Apr 19 '17 at 13:07
  • 2
    clearly the error on line 10 would've output `ReferenceError: error is not defined` as soon as you try to get that file - if you are not seeing this error, then you aren't even "hitting" the server correctly - so ... what URL are you typing in your browser? – Jaromanda X Apr 19 '17 at 13:10
  • Whats that output of `console.log(filepath)`? – Saif Jun 29 '18 at 10:01

0 Answers0