I'm very new to node.js so I apologize if this is a poor question. I have a simple HTTP server here:

var http = require('http');

http.createServer(function (request, response){

    response.writeHead(200, {"Content-Type":"text/html"});
//  response.write("<html><head><script type = 'text/javascript'>alert('hello');</script></head></html>");
    response.write("<html><head><script type = 'text/javascript' src = 'alerter.js'></script></head></html>");
    response.end();
}).listen(8000);

console.log("Server has started.");

alerter.js contains the one line:

alert("hello");

Why does using the commented-out line properly cause an alert, but calling alerter.js does nothing?

share|improve this question
What do you see in Firebug? This is probably a relative path issue. – SLaks Jun 10 '12 at 3:22
3  
Is your node.js set up to serve out alerter.js when the browser asks for it? – Paul Tomblin Jun 10 '12 at 3:23
@PaulTomblin I'm sorry, I don't know if it is, how would I check that? – purpleladydragons Jun 10 '12 at 3:26
2  
What @PaulTomblin is asking is if the web server you just wrote in javascript responds to requests for /alerter.js with the file you intend. The main concept in Node.js is that you are writing a web server, it's behavior is dependent on what you tell it to do and add support for. There are frameworks that make this easier, but you're approach is the best way to learn the concepts. As a hint, look at req.uri and the uri parsing library or more intermediate tutorials. – Timothy Meade Jun 10 '12 at 3:43
@TimothyMeade I'm really sorry, but I'm struggling to understand. When I request localhost:8000/alerter.js I get an empty page. Is uri for parsing the url, so I could in turn check the pathname for /alerter.js? If this is the case, then how would I execute the javascript file from there? I guess I'm asking how I can make node.js or my server aware of the alerter.js file and know to look there. – purpleladydragons Jun 10 '12 at 3:53

2 Answers

up vote 4 down vote accepted

Your problem, as Paul and Timothy pointed out, is that when the browser gets your html with an external reference, it goes back to the server and asks it for alerter.js, expecting to have the script delivered to it. However, your server always sends out the same thing regardless of what the request asks for, so the browser never gets the script.

You need to change your server to, at the very least, handle a request for alerter.js. Realistically, I'd use something like Connect's static file handler to do that; put all the assets like scripts, stylesheets and images in a directory (usually called "public") and tell the static handler to serve them from there.

share|improve this answer

You are missing a ' to end the type attribute.

share|improve this answer
You're right, however, it still does not work with the ' added. – purpleladydragons Jun 10 '12 at 3:27

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.