6

I am a complete node.js newbie and struggling with the basics.I have a html file and I would like to call external javascript file in the html page using node.js in local host environment.

Pradeep Raj
  • 339
  • 2
  • 5
  • 12
  • What Node.js have to do with html? There are Node.js modules to emulate web browser behavior, but are you sure it's really what you want? – Leonid Beschastny Nov 12 '13 at 10:41
  • 1
    Could you tell us what you're trying to achieve? Also, please read the [XY Problem](http://meta.stackexchange.com/a/66378) to better define your question. – Paul Mougel Nov 12 '13 at 11:48

3 Answers3

1

Your main file should check whether the requested url is asking for html or js or css file.

var server = http.createServer(function(request, response) {
console.log("Received Request: " + request.url);
    if(request.url.indexOf('.html') != -1) {
        fs.readFile("game" + request.url, function (error, data) {
            if (error) {
                response.writeHead(404, {"COntent-type":"text/plain"});
                response.end("No Html Page Found.");
            } else{
                response.writeHead(200, {'Content-Type': 'text/html'});
                response.write(data);
                response.end(); 
            }

        });
    }
    else if(request.url.indexOf('.js') != -1) {
        fs.readFile("game" + request.url, function (error, data) {
            if (error) {
                response.writeHead(404, {"COntent-type":"text/plain"});
                response.end("No Javascript Page Found.");
            } else{
                response.writeHead(200, {'Content-Type': 'text/javascript'});
                response.write(data);
                response.end(); 
            }

        });
    }
    else if(request.url.indexOf('.css') != -1) {
        fs.readFile("game" + request.url, function (error, data) {
            if (error) {
                response.writeHead(404, {"COntent-type":"text/plain"});
                response.end("No Css Page Found.");
            } else{
                response.writeHead(200, {'Content-Type': 'text/css'});
                response.write(data);
                response.end(); 
            }

        });
    }
    else {
        console.log("Inside the inside else statement");
        response.writeHead(404, {"COntent-type":"text/plain"});
        response.end("No Page Found");
    }
}); 

After That you can include external javascript and css files in html files.

0

You just add the script tag into the html:

<script type="text/javascript" href="/path/to/src.js"></script>
Adam Hopkinson
  • 28,281
  • 7
  • 65
  • 99
  • This will execute the script client-side, not *"using node.js in local host environment"*. – Paul Mougel Nov 12 '13 at 10:44
  • Oh, I see that - I'd read "would like to call external javascript file in the html page". I think it could be read either way, but I did think the question was a little simplistic with my interpretation! – Adam Hopkinson Nov 12 '13 at 11:41
0

I think I have understood your real interest.

<script src="script.js"></script>

If you are trying to get a script tag (like that one above) working on your html, you should write a "router" inside your Node.js http.createServer callback. When the browser find the script tag, it will send a specific request to the server and then you can send the javascript code back on the response.

I have written a way to do it on a similar question here: How to include javascript on client side of node.js?.

Community
  • 1
  • 1
Samuel
  • 1,271
  • 1
  • 15
  • 29