why it is not calling a.html

Go To StackoverFlow.com

-1

 var http = require('http');
 http.createServer(function (req, res) {

if (req.url == '/') 
{
    req.url += "a.html";
    //facebookAPI(req,res);
}

    }).listen(1337);

when I typed the address in the browser it was not calling that url. Any thought Thank you.

2012-04-05 18:37
by draford
Changing the request URL isn't going to do anything for you. Why don't you explain what you're trying to do first - Timothy Strimple 2012-04-05 18:39
I am trying to display a.html on the browser For example you type 10.0.0.1:1337 in the address box it shows a.html which has hello wor - draford 2012-04-05 18:57


1

Here is how you could serve that file. This is untested!

var http = require('http');
var fs = require('fs');
http.createServer(function (req, res) {
    if (req.url == '/') 
    {
        fs.readFile('a.html', 'utf8', function(err, data) {
          if(err) {
            res.end('Error Loading File');
            return;
          }

          res.end(data);
        });
    } else {
        res.writeHead(404, {'Content-Type': 'text/plain'});
        res.end('file not found');
    }
}).listen(1337);

There are better ways to accomplish the same thing, but this should get you started.

2012-04-05 19:05
by Timothy Strimple
Definitely look into doing things this way, the fs seems to me to be the way to serve files you read from disk. Also, if you're going to be doing more web pages, you can consider using the Connect, Express, and Railway node extensions, they seem to be the de-facto suggestions to serve web pages in node - Will Buck 2012-04-05 19:11
Ads