Javascript 将本地js函数导入node.js
我在这个问题上停留了一段时间,认为这是因为我误解了node.js中文件的链接 我的结构如下:Javascript 将本地js函数导入node.js,javascript,jquery,html,node.js,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.1
./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>
我的节点脚本应该服务于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);
它说ReferenceError:$未定义
,我认为这是因为它没有引用JQuery,而是将其更改为document.getElementById('p1')。innerHTML='this worked'代码>无法解决此问题
如何在我的节点应用程序中运行Javascript/JQuery?节点是一种服务器语言,无法访问DOM。您可以创建节点服务器,它将承载HTML和JS文件,但您将无法在节点中运行客户端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>
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);
想想node_test.js
中的这一行需要什么才能工作
dep1.print_to_p();
函数的代码引用jQuery$
变量,但由于js_test.js
没有定义变量,因此会导致错误
另一件事,即使定义了$
,它也会停止不知道html页面本身。这是正在服务器上运行的Node.js。例如,如果使用“服务器友好的jQuery”实现,效果会更好。或者,如果您想将该段JavaScript代码移动到HTML页面上运行
你接近了,继续努力。缺少的链接用于了解服务器上正在运行的内容和浏览器上正在运行的内容。问题在于,http请求无法处理链接文件,因为它们都被解释为html文件。
如果让服务器使用相同的内容类型处理不同的文件类型,则依赖关系不会正确传递。您可以使用诸如或之类的库或包(这有助于单独标识内容类型)来解决此问题,也可以通过标识依赖文件的内容类型并调整请求来手动解决此问题
这样做的一个例子是修改节点_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请求,这意味着必须处理和读取每个文件。
为了获得正确的文件扩展名,可以使用名为的节点包
dep1.print_to_p();
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);