2

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?

5
  • What do you see in Firebug? This is probably a relative path issue. Commented Jun 10, 2012 at 3:22
  • 3
    Is your node.js set up to serve out alerter.js when the browser asks for it? Commented Jun 10, 2012 at 3:23
  • @PaulTomblin I'm sorry, I don't know if it is, how would I check that? Commented Jun 10, 2012 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. Commented Jun 10, 2012 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. Commented Jun 10, 2012 at 3:53

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

Comments

3

You are missing a ' to end the type attribute.

1 Comment

You're right, however, it still does not work with the ' added.

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.