0

Hi Tried few tutorials on running php on node from this site scriptol.com so I was successful on loading the page but the problem is that some of the images did not loaded and the css rule is not also running any idea why?

Here is my code for running the server under node:

    http = require("http"),  
path = require("path"),  
url = require("url"),  
fs = require("fs");  

function sendError(errCode, errString, response)
{
    response.writeHead(errCode, {"Content-Type": "text/plain"});  
    response.write(errString + "\n");  
    response.end(); 
    return; 
}

function sendFile(err, file, response) 
{  
  if(err) return sendError(500, err, response);
  response.writeHead(200);  
  response.write(file, "binary");  
  response.end();    
}  

function getFile(exists, response, localpath)
{
  if(!exists) return sendError(404, '404 Not Found', response);
  fs.readFile(localpath, "binary", 
    function(err, file){ sendFile(err, file, response);});   
}

function getFilename(request, response)
{  
    var urlpath = url.parse(request.url).pathname; // following domain or IP and port  
    var localpath = path.join(process.cwd(), urlpath); // if we are at root 
    path.exists(localpath, function(result) { getFile(result, response, localpath)});  
}

var server = http.createServer(getFilename);
server.listen(1000);  
console.log("Server available...");  

I have also made sure that all the files are existing, Please help Thanks!

6
  • Basically I'm really new to node so please dont be too hard on me.. :) Commented Aug 1, 2014 at 1:23
  • uh... but that's an http server. Why do you say you're running php? Commented Aug 1, 2014 at 1:24
  • because on my addressbar I typed this http://localhost:1000/message.php am I not runnning php? Commented Aug 1, 2014 at 1:26
  • you might be able to display the contents of message.php as plain text or html, but not interpreted php. However, you can use the chrome console, click the network tab and see what requests are being done and why aren't they loading. Commented Aug 1, 2014 at 1:32
  • thats weird I tried running it on chrome and it didnt load the whole page instead it just displayed the whole code of the page.. is this because of chrome? Commented Aug 1, 2014 at 1:36

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.