不能以正确的相对方式使用 "src" 标签

can't use "src" tag in a proper relative way

我的目录结构如下。 "--"表示文件夹中的单个级别。

--file_uploads
----routes
----views
------index.html
------app.js
------angular.min.js
----public
----node_modules
----app.js
----package.json

在 file_uploads 文件夹中,我打开终端并写入 "sudo nodemon app",我的服务器启动 运行,当我转到 "localhost://3000" 时,我得到两个错误,即 "error 404 cannot find angular.min.js" 和 "error 404 cannot find app.js".

在我的 index.html 中,我有这两行源代码 "src" :-

<script type="text/javascript" src="./views/angular.min.js"></script>

<script src="./views/app.js"></script>

请指导我哪里做错了,我的 index.html 页面打开但找不到 angular.min.js 和 app.js 的相对路径。

好的,我有一个从 http 服务器切断文件的解决方案,但我需要用我的 express 我可以服务器 html 代码和 angularjs 代码。如何实现express+node+angular+html?

在我的 app.js 服务器端,我正在这样做。

var cons = require('consolidate');

// view engine setup
app.engine('html', cons.swig);
app.set('views', path.join(__dirname+ "/views"));
app.set('view engine', 'html');
app.use(express.static(path.join(__dirname, 'public')));
app.use('/', index);
app.use('/users', users);
app.use('/profile',profile);

当谈到 index.js 我这样做 :-

router.get('/', function(req, res, next) {
res.render('index.html');
});

PS,我的服务器正确呈现 index.html。

使用这个,因为你使用的是 express

app.use(express.static('public'))

这将提供 public 文件夹中的所有文件。将你的 /angular.min.js/app.js 放在那里,你的 index.html 应该会捡起它们。当然,您可以选择提供 views 文件夹,但其中的 index.html 可能会发生冲突。

确保脚本从根指向,例如/angular.min.js/app.js

  1. 安装:npm install -g http-server。
  2. cd 进入 views
  3. 运行 http-server
  4. 在浏览器中转到 http://localhost:8080/

Nodemon 用于提供服务器文件,而不是您尝试提供的客户端文件。