Reputation: 833
Inside my html file, I am using this:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
However, I was not allowed to connect to internet during the running. How can I use node js to return this file locally. I already download this file "jquery.min.js" in my local machine.
I am trying to use this code, but it does not work.
function callback(data) {
res.writeHead(200, {"Content-Type": "text/html"});
var str = data+"";
res.write(str);
res.end();
}
HTMLtemplate.load("jquery.min.js", callback);
Upvotes: 0
Views: 3125
Reputation: 22553
You can't easily serve up both the web page and jquery in a simple node server that looks like this:
var http = require('http');
var server = http.createServer(function (request, response) {
response.writeHead(200, {"Content-Type": "text/plain"});
response.end("Hello World\n");
});
server.listen(8000);
I think you are going to need some router or framework to serve up multiple http endpoints and static assets. Many people use express:
Or even something as simple as this:
https://github.com/creationix/node-router
I suppose you could serve up a web page with jquery embedded in it using a naked node server by doing something like this (this is only psuedo-code):
var http = require('http');
var fs = require('fs');
var html = "<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<script>REPLACEME</script>
</head>
<body>
</body>
</html>";
var server = http.createServer(function (request, response) {
fs.readFile('jquery.js', 'utf8', function (err,data) {
response.writeHead(200, {"Content-Type": "text/html"});
response.end(html.replace('REPLACEME', data));
});
});
server.listen(8000);
But that seems very wrong except, perhaps, as an exercise that demonstrates what a web framework does. Unless you are doing this for school, just install one of the above frameworks (or some other) and it will be easy.
Upvotes: 1