Hello, I'm trying to do web server which automatically opens index.html but 
failed
this is my code

var http = require('http');
var url = require('url');
var fs = require('fs');
const por = 8080;

//Create a server
http.createServer( function (request, response) {
 // Parse the request containing file name
 var pathname = url.parse(request.url).pathname;

 // Print the name of the file for which request is made.
 console.log("Request for " + pathname + " received.");

 // Read the request file content from file system
 fs.readFile(pathname.substr(1), function (err, data) {
 if (err) {
 //HTTP status: 404

 //print error
 console.log(err);
 response.writeHead(404, {'Content-type': 'text/html'});

 } else {
 //Page found, HTTP Status: 200 : OK

 response.writeHead(200, {'Content-type': 'text/html'});
 switch (request.method){
 case "GET":
 if (request.url !== "/") {
 console.log("Another");
 response.write(data.toString());
 } else {
 console.log("Index");
 }
 break;
 default:
 break;
 }

 }response.end();
});

}).listen(por);
console.log("Server runing on http://1270.0.1:8080";);

When type localhost:8080 throw me error

Request for / received.
{ Error: ENOENT: no such file or directory, open ''
 at Error (native) errno: -2, code: 'ENOENT', syscall: 'open', path: '' }

I know if type localhost:8080/index.html will work, but that's not the effect 
I'm looking for.

-- 
Job board: http://jobs.nodejs.org/
New group rules: 
https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules: 
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
--- 
You received this message because you are subscribed to the Google Groups 
"nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/nodejs/58b80116-c383-44d8-98f5-045e997bf628%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to