节点.js:加载HTML文件中包含的.js和.css文件,而不使用Express/框架
Node.js : Load .js and .css files included in HTML file without Express/frameworks
如何加载与 Node.js 一起显示的 HTML 文件中包含的.js .csv和.css文件?(没有像Express这样的框架)
例如 <script src="clock.js"></script>
在 Firefox 中,它显示页面 fs.readFile('./index.html') 请求 clock.js、jquery.js 等作为 html 文件,可能是因为 response.writeHead 是 text/html。
有没有办法做到:
if file included in index.html = .js
set writeHead to application/javascript
if file included in index.html = .css
set writeHead to text/css
没有事先指定索引.html可以包括哪些文件?(例如,如果将来 index.html 也使用 menu.js,则无需更新 node.js 脚本)
有没有办法查看index.html请求的文件,然后修改这些文件的标头(例如.js有application/javascript)?
这是你要找的那种东西吗?
var http = require("http");
var fs = require("fs");
var path = require("path");
var server = http.createServer(function(request, response) {
//typically what will happen here is that you will see
//see the request for the page (index.html?) first and
//then the browser's subsequent requests for js, css etc files
var url = request.url;
var file = //do something clever to extract the file name from the url ( url.split("/")?)
var html = fs.readFileSync(file,"utf-8");
//probably want to check the file extension here to determine the Content-Type
response.writeHeader(200, {"Content-Type": "text/html"});
response.write(html);
response.end();
});
server.listen(8081);
console.log("Server is listening");
有点农业,你可能会遇到各种路由问题 - 但这就是这个想法。
相关文章:
- 从文本文件js中读取列表
- 当我用ctrl+p搜索时,为什么要在谷歌chrome上使用inspect来调试缺少显示文件js
- 获取用于外部JavaScript文件(.js)的代码提示
- 如何将Javascript文件(.js文件)放在web服务器中
- 如何将 JavaScript 文件(.js 文件)添加到 C# 项目并从 C# 代码调用 JavaScript 方法
- 在 Node 中包含 javascript 文件.js无需 require('.') ing
- 如何使用jquery显示多个文件.多文件.js
- 使用 PDF 打开 PDF 文件.js而不将文件作为查询字符串传递
- 转换颜色十六进制值以用于三个 JSON 文件.js
- 如何使用 jQuery 选择多个文件.多文件.js插件?使用单个关闭按钮一次选择多个文件
- 如何在 Gulp 任务中间添加 CSS 文件.js
- 如何从Dropzone上传和删除文件.js
- 如何在视频中添加视频文件.js 4.2.1.
- 从快速节点下载文件.js具有 JWT 授权
- 访问 JavaScript 文件.js中的 C# 变量
- 在节点中热重载外部 js 文件.js如果文件有任何更改
- 如何在一台服务器中运行不同的.js文件.js NODE JS中的文件
- 如何在多文件.js中上传文件时检查文件扩展名
- 将文件.js中的var导入到模块joomla的模板中,并使用后台进行修改
- 在path项目中编写文件js脚本