javascript - Node.js return a JS file locally -


inside html file, using this:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script> 

however, not allowed connect internet during running. how can use node js return file locally. download file "jquery.min.js" in local machine.

i trying use code, 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);  

you can't serve both web page , jquery in simple node server looks 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 going need router or framework serve multiple http endpoints , static assets. many people use express:

http://expressjs.com/

or simple this:

https://github.com/creationix/node-router

i suppose serve web page jquery embedded in using naked node server doing (this 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 seems wrong except, perhaps, exercise demonstrates web framework does. unless doing school, install 1 of above frameworks (or other) , easy.


Popular posts from this blog