带有readFile html的node.js,但不加载/查找jquery文件 [英] node.js with readFile html but not loading/finding jquery file

查看:214
本文介绍了带有readFile html的node.js,但不加载/查找jquery文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法加载我的jquery文件或任何文件在node.js中读取html文件。我花了相当一段时间在这个,并注意到基于Web的谷歌托管库文件工作正常,但我的本地文件没有。我无法弄清楚为什么,也许我没有把它引导到正确的目录,但我不确定我还会把它放在哪里或指示它。



目录包括这3个文件

  index.html 
jquery.min.js
loadfile.js

index.html
注释掉的谷歌托管的jquery文件工作得很好。但是本地没有。


 <!DOCTYPE html> 
< html lang =en-US>
< head>
< meta charset =UTF-8>
< title>< / title>
< script type =text / javascriptsrc =jquery.min.js>< / script>
<! - < script src =// ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js\">< ;; script> - >
< script type =text / javascript>
$(document).ready(function(){
alert(test);
});
< / script>
< / head>
< body>
嗨!
< / body>
< / html>

这是我加载节点的文件。
loadfile.js

  var http = require('http'); 
var url = require('url');
var fs = require('fs');


server = http.createServer(function(request,response){
current_url = url.parse(request.url).pathname;

file_path = __dirname + current_url;

var tmp = file_path.lastIndexOf('。');
var ext = file_path.substring(tmp + 1);

console.log(ext);

if(ext =='js')
response.writeHead(200,{'Content-type':'text / javascript'});
else if(ext =='css')
response.writeHead(200,{'Content-type':'text / css'});
else
response.writeHead (200,{'Content-type':'text / html'});

if(current_url =='/'){
fs.readFile('index.html', 'utf8',函数(错误,内容){
response.end(contents);
});
} else {
response.end();
}

});

server.listen(8000);
console.log(在端口8000上运行在本地主机上);

我得到这个错误Uncaught ReferenceError:$没有在$(document).ready行中定义它似乎并不认为我加载了jQuery。



任何帮助将不胜感激。我总是可以使用谷歌托管的文件,但这并不适合我。 c> current_url 不是/,现在你的服务器只能发送文件内容 index.html 给客户端。



您需要在else子句中具有以下内容:

  fs.readFile(file_path,'utf8',function(errors,contents){
response.end(contents);
});


I'm having trouble loading my jquery file or any file for that matter in a node.js read html file. I spent quite some time on this and noticed that the web based google hosted library file works fine, but my local file does not. I cannot figure out why, perhaps I'm not directing it to the correct directory, but I'm not sure where else I would put it or direct it.

Directory includes these 3 files

index.html
jquery.min.js
loadfile.js

index.html The commented out google hosted jquery file works just fine. But the local does not.

<!DOCTYPE html>
<html lang="en-US">
    <head>
        <meta charset="UTF-8">
        <title></title>
        <script type="text/javascript" src="jquery.min.js"></script>
        <!-- <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>  -->
        <script type="text/javascript">
            $(document).ready(function() {
                alert("test");
            });
        </script>
    </head>
    <body>
        Hi!
    </body>
</html>

Here is the file I load with node. loadfile.js

var http = require('http');
var url = require('url');
var fs = require('fs');


server = http.createServer(function (request, response) {
    current_url = url.parse(request.url).pathname;

    file_path = __dirname + current_url;    

    var tmp = file_path.lastIndexOf('.');
    var ext = file_path.substring(tmp + 1);

    console.log(ext);

    if(ext == 'js')
        response.writeHead(200, {'Content-type': 'text/javascript'});
    else if(ext == 'css')
        response.writeHead(200, {'Content-type': 'text/css'});
    else
        response.writeHead(200, {'Content-type': 'text/html'});

    if(current_url == '/') {
        fs.readFile('index.html', 'utf8', function (errors, contents) {
            response.end(contents); 
        });
    } else {
        response.end();
    }

});

server.listen(8000);
console.log("Running in localhost at port 8000");

I get this error Uncaught ReferenceError: $ is not defined on the $(document).ready line since it doesn't seem to think I loaded the jquery.

Any help would be appreciated. I could always use the google hosted file, but this doesn't sit well with me. Thanks in advance!

解决方案

You haven't actually serve the file if the current_url isn't "/", right now your server can only send the file content of index.html to the client.

You need to have something like the following in your "else" clause:

fs.readFile(file_path, 'utf8', function (errors, contents) {
    response.end(contents); 
});

这篇关于带有readFile html的node.js,但不加载/查找jquery文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆