- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
当我从我的nodejs Web服务器提供一个包含一些javascript的html文件时,与我从外部源包含相同的javascript时相比,我得到了不同的结果。我已经验证,使用 javascript 内联或外部源直接打开 html 文件的效果与预期相同。在下面的示例中,我在 h1 标记中得到“Modified header”,而使用 javascript 作为外部源,我在 h1 标记中得到“Unmodofied header”。有人可以解释如何解决这个问题吗?谢谢。
nodejs Web 服务器代码:
var http = require('http')
var fs = require('fs')
http.createServer(function (request, response) {
fs.readFile('htmlfile.html', function(err, data) {
response.writeHead(200, {'Content-Type': 'text'})
response.write(data)
response.end()
})
}).listen(8081)
htmlfile.html如下:
<html>
<body>
<h1 id = "header"> Unmodified header</h1>
<!-- <script src="populate_header.js"></script> -->
<script>
function populate_header () {
document.getElementById("header").innerHTML = "Modified header"
}
populate_header ()
</script>
</body>
</html>
最佳答案
简而言之,您的 http 服务器未配置为在浏览器请求时将 populate_header.js
发送到浏览器。
当您在 HTML 文件中执行此操作时:
<script src="populate_header.js"></script>
您告诉浏览器向您的网络服务器发送一个名为 populate_header.js
的资源请求。但是您的 Web 服务器没有一个请求处理程序来查看正在请求的文件并为该特定资源提供服务。无论请求什么资源,您的网络服务器始终发送 htmlfile.html
。因此,浏览器会请求脚本文件并获取 HTML 文件(导致浏览器基本上忽略它)。因此,populate_header.js
中的 Javascript 永远不会传递到浏览器,因此其中的脚本永远不会运行。
当您内联包含脚本时,Javascript 会随 HTML 一起交付,并且可以正常工作,无需在 Web 服务器上设置其他路由。
默认情况下,node.js Web 服务器根本不提供任何文件。它仅提供您为其创建路由处理程序的文件。可以创建一个为大量静态文件提供服务的路由(使用 Express 框架时,express.static()
正是这样做的)。但是,默认情况下,它不提供任何文件。
一旦您需要多个路由处理程序,我建议您使用一个非常简单的框架,例如 ExpressJS,因为它会节省您大量时间并且非常轻量级。但是,如果您要向现有的小型 Web 服务器添加新的路由处理程序,您可以这样做:
var http = require('http')
var fs = require('fs')
http.createServer(function (request, response) {
if (request.method === "GET") {
let fname;
// Look at what resource was requested and match that up
// with the appropriate file name
// DO not accept any resource requested because that could open up
// your server for people to request your actual server code files
switch(request.url) {
case "/":
fname = "htmlfile.html";
break;
case "/populate_header.js":
fname = "populate_header.js";
break;
default:
break;
}
if (fname) {
fs.readFile(fname, function(err, data) {
if (err) {
response.writeHead(404);
response.end();
} else {
response.writeHead(200, {'Content-Type': 'text'})
response.write(data)
response.end();
}
});
} else {
response.writeHead(404);
response.end();
}
}
}).listen(8081)
在这里,您可以看到您正在查看 request.url
以查看到底请求了什么,然后发送该资源。它还寻找一个仅响应 GET 请求的 request.method。并且,当发送其他文件时,它会发送 404 响应。
使用 ExpressJS 框架,这一切都会简单得多。
const express = require('express');
const app = express();
// look for matching static resources in the static_files subdirectory
app.use(express.static("static_files"));
// send the htmlfile.html file when / is requested
app.get("/", function(req, res) {
res.sendFile("static_files/htmlfile.html");
});
app.listen(8081);
然后,只需在主服务器目录下名为 static_files
的子目录中找到所有静态资源,Express 框架就会自动在该子目录中查找与请求的 URL 匹配的文件。此代码为 /
添加了一个自定义路由,专门发送 htmlfile.html
文件,您当然可以根据需要进行自定义。
有关 Node.js 服务器默认情况下如何不发送任何文件的进一步讨论,请参阅以下其他相关答案:
Can't load local files when using NodeJS server
关于javascript - Nodejs HTTP Createserver - 从 html 文件提供服务时不可预测的 javascript 执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52284578/
我的 React 项目需要更新 nodejs。那么我如何将我的 Node js 重新安装到 Ubuntu 16.04 中的最新版本。 我当前的 Node 版本是 node -v v6.0.0 我当前的
我正在寻找逐步调试 NodeJS 服务器代码的有效方法。目前我使用了几十个console.log(),这非常困难。完美的工具可以让我检查堆栈中每个变量的值并逐行跟踪我的程序。首选操作系统 = MacO
我的网站上有以下两个文件: firebase.js gridsome-server.js firebase.js 是一个“常规”javascript 文件,包含以下内容: import firebas
我有一个nodejs应用程序从文件夹A执行,二进制X也在文件夹A中执行(使用子进程exec)。二进制 X 在文件夹 A 中生成输出文件 O,因此始终从调用位置开始。 我需要nodejs应用程序来在仲裁
我有以下nodeJS服务器,它似乎工作正常。我想编写一个客户端,从服务器接收消息并根据消息调用一些 JS。 涉及的步骤是: 用户访问网址http://server.xyz.com:8080/pa no
我想从 Node 服务器进行其余 api 调用。我目前脑子里有请求模块。 您是否会建议用于 Nodejs 中生产实践的 REST 调用(get/post)的任何其他最佳模块? 问候,公羊 最佳答案 R
我正在尝试像这样使用 mainModule: const { mainModule } = require('process'); module.exports = path.dirname(main
我现在对那些版本号真的很困惑。我正在计划一个新项目,想知道这两个版本之间有什么区别。这两个版本之间似乎有很大的跳跃,但现在我找不到区别。 使用 4.1 版安全吗? 感谢您的帮助! 最佳答案 跳转到 v
我试图找到我的问题的解决方案,但找不到,并且正在寻找一些“最佳实践示例”。我有一个 nodejs express 应用程序,我的函数在文件中拆分。例如我有这个 Controller (oktacont
这看起来像是一个非常简单的问题,但作为一个 JS 初学者,我想知道是否可以在 webextension 中使用 NodeJS 模块(例如我想使用这个:https://github.com/yaronn
我有一个文件。a.js class A{ constructor(name){ this.name = name; } displayName(){ conso
我想做的是这样的: node x.js | node y.js 文件 x.js 只是打印一个字符串: console.log("hi"); 文件 y.js 旨在通过 process.stdin 获取字
对于这个新的nodejs debugger I am working on我想对显示的源代码行进行着色。有什么关于 npm 使用的建议吗? 有很多语法荧光笔,但使这种情况有点不同的是 输出是到终端;它
有没有什么方法可以从 ejs View 中引用包含在 node_modules 文件夹中的 Nodejs 库? 我正在使用 expressjs 并且我的客户端库由 /public 文件夹提供,如下所示
我是 NodeJS 的新手,我正在尝试根据 NodeJS 站点上的指南在 NodeJS 中创建一个服务器。我已经在我的电脑上安装了 NodeJS 并使用以下代码制作了 app.js 文件。 const
我有一个 nodejs-express 服务器 (1) 与 mongodb 通信,还有一个 web 服务器 (2) 在 nodejs-express 和 Angularjs 中。我正在尝试发出 pos
我一直在解决(firebase 和 nodejs)问题,这是该问题的第四部分,如何在登录到 server.js 后传递数据 我已经尝试过this但未能使其正常工作。 基本上,我正在尝试将用户idTok
每次页面刷新时,NodeJS 都会在套接字上多次写入数据。当我刷新页面时,nodejs 服务器写入套接字的计数增加,在多个页面刷新时,写入计数固定为 3。 请检查控制台输出是否有此奇怪的响应。请提出同
我在尝试更新文件夹并再次部署其内容时遇到问题。我必须使用 NodeJS 并已获得端口 8080 来使用。我尝试创建一个 php 脚本(update.php): 现在我想启动NodeJS脚本进行更新,
我不明白java多线程系统和Nodejs多线程系统在性能和资源共享方面的区别。由于 NodeJS 为您的程序使用事件循环单线程,但在幕后,它将任务分配给不同的线程,如文件读取或数据库查询。所以它使用多
我是一名优秀的程序员,十分优秀!