【问题标题】:Why my jQuery, files are not loaded?为什么我的 jQuery,文件没有加载?
【发布时间】:2018-08-15 04:01:12
【问题描述】:

我导入 jQuery,像这样在我的脑海中进行 boostrap

<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
<link rel="stylesheet" type="text/css" href="css/style.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"</script>

我正在尝试像这样使用 NodeJS 和 Express,我在 Google 上找到了它

var http = require('http');
var fs = require('fs');
var express = require('express');
var app = express();

http.createServer(function(req, res) {
fs.readFile('main.html', function(err, data) {
res.writeHead(200, {
  'Content-Type': 'text/html',
  'Content-Length': data.length
});
res.write(data);
res.end();
});
}).listen(8000);

但是当我的页面被加载时,那些库并没有被加载。 我也有一些图片

    <div class="item active">
          <a href="vungtau.html">
          <img src="IntroduceImage/VungTau.jpg" alt="Vũng Tàu">
          <div class="carousel-caption">
            <h3>Vũng Tàu</h3>
            <h4>Lần đầu đi Vũng Tàu bằng xe máy</h4>
          </div>
          </a>
        </div>

它们也没有加载。这是地址:D:\Atom Project\IntroduceImage\VungTau.jpg

有人知道为什么吗? 我在没有 NodeJS 的情况下加载它,例如 file:///D:/Atom%20Project/main.html。他们都加载了。 请帮忙,我是 NodeJS 的新手。

【问题讨论】:

  • 通过 express 文档有 express.static 来配置应用程序的根路径,然后根据该相对路径给文件参考
  • 请参考link 将使用 NodeJS + Express 的文件包含到您的项目中。
  • 谢谢@ManpreetMatharu,与您的链接进行比较,并尝试了一些搜索。我做到了。

标签: javascript jquery node.js express


【解决方案1】:

你必须提供express.js-staticFile中给出的文件参考路径

类似的,

const path = require('path');

app.use(express.static('./'));
app.use(express.static(path.join(__dirname, 'public')));

我的图像存储在服务器的根目录中。

【讨论】:

  • 真的需要“pp.use(express.static(path.join(__dirname, 'public')));”吗?我只使用“app.use(express.static('./'));”,效果很好
  • 如果您在公共目录中有外部文件,那么,
  • @NguyenHoangVu-K11FUGHCM 欢迎
【解决方案2】:

提示如下

app.use('/static', express.static(path.join(__dirname)));

http://localhost:3000/static/images/kitten.jpg
http://localhost:3000/static/css/style.css
http://localhost:3000/static/js/app.js
http://localhost:3000/static/images/bg.png
http://localhost:3000/static/hello.html

通过这个链接

https://expressjs.com/en/starter/static-files.html

【讨论】:

    猜你喜欢
    • 2013-06-16
    • 2013-02-28
    • 1970-01-01
    • 2016-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-11
    • 1970-01-01
    相关资源
    最近更新 更多