将本地js函数导入node.js

时间:2016-08-11 11:08:37

标签: javascript jquery html node.js

我现在停留了一段时间并认为这是因为我误解了node.js中文件的链接。

我有以下结构:

./main_test.html
./js_test.js
./node_test.js

main_test.html是:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
</head>
<body>
<p id="p1">
</p>
</body>
</html>

js_test包含一个函数,用于将文本打印到ID为<p>的{​​{1}}标记,并且是:

p1

我的节点脚本应该为module.exports.print_to_p = function(){ $("#p1").text('This worked.'); }; 提供服务,并从main_test.html运行该功能:

js_test.js

它说var http = require('http'), fs = require('fs'), $ = require('jQuery'), port = 1337, dep1 = require('./js_test.js'); dep1.print_to_p(); var app = http.createServer(function(request, response) { fs.readFile("main_test.html", function(error, data) { if (error) console.log(error); response.writeHead(200, { 'Content-Type': 'text/html' }); response.write(data); response.end(); }); }).listen(port); 我认为是因为它没有引用JQuery,但将其更改为ReferenceError: $ is not defined并没有解决这个问题。

如何在我的节点应用程序中运行Javascript / JQuery?

3 个答案:

答案 0 :(得分:1)

问题是您的http请求无法处理链接文件,因为它们都被解释为html文件。 如果让服务器使用相同的内容类型处理不同的文件类型,则无法正确传递依赖项。您可以使用某些库或包expressmime-type(这有助于单独识别内容类型)来修复此问题,或者您可以通过识别相关文件的内容类型来手动执行此操作。调整你的要求。

执行此操作的示例是修改node_test.js,如下所示:

var http = require('http'),
fs = require('fs'),
port = 1337,
path = require('path');

var app = http.createServer(function (request, response) {
    var filePath = request.url;
    var extension = path.extname(filePath);
    var contentType = 'text/html';  // your default content-type

    switch (extension) {
        case '.js':
            contentType = 'text/javascript';
            break;

        ...  // here you can specify the content-types you need 
    }

    fs.readFile(filePath, function(error, data) {
        if (error) console.log(error);
        response.writeHead(200, {
            'Content-Type': contentType
        });
        response.write(data);
        response.end();
    });
}).listen(port);

请注意,您还必须在此处指定要读取的文件!为每个依赖文件独立调用http请求,这意味着必须处理和读取每个文件。 为了获得正确的文件扩展名,您可以使用名为path的节点包。

答案 1 :(得分:0)

Node是一种服务器语言,无法访问DOM。您可以创建节点服务器,它将托管HTML和JS文件,但您无法在Node中运行客户端JS代码。

您的代码应为:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script src="js_test.js"></script>
<script> print_to_p(); </script>
</head>
<body>
<p id="p1">
</p>
</body>
</html>

js_test.js

function print_to_p(){
$("#p1").text('This worked.');
};

node_test.js

var http = require('http'),
fs = require('fs'),
port = 1337,

var app = http.createServer(function(request, response) {
fs.readFile("main_test.html", function(error, data) {
    if (error) console.log(error);
    response.writeHead(200, {
        'Content-Type': 'text/html'
    });
    response.write(data);
    response.end();
});
}).listen(port);

答案 2 :(得分:0)

想一想node_test.js中这一行需要什么才能发挥作用。

dep1.print_to_p();

该函数的代码引用了jQuery $变量,但由于js_test.js没有定义一个,因此会导致错误。

另一件事,即使定义$,它也会停止不知道html页面本身。这是在服务器上运行的Node.js.如果您使用&#34;服务器友好的jQuery&#34;它会更好。实施,cheerio例如。或者,如果您要移动那段JavaScript代码以在HTML页面上运行。

你关闭了,继续努力。缺少的链接是实现服务器上运行的内容以及浏览器上运行的内容。