首页 > 解决方案 > 找不到 Angularjs 和引导库文件 (404) http-serve

问题描述

我正在创建一个小应用程序来学习 angularjs 并使用 npm 安装它和其他依赖项。为了为应用程序提供服务,我在本地通过 npm 安装了 http-server 包。我的应用程序目录结构看起来像这样

餐厅 > 应用程序、node_modules、package.json

应用程序文件夹看起来像

应用 > index.html、app.js

在我的 index.html 中,我尝试像这样从 node_modules 引用 angular.min.js 和引导 css 文件

<link rel="stylesheet" href="../node_modules/bootstrap/dist/css/bootstrap.min.css">
<script src="../node_modules/angular/angular.min.js"></script>
<script src="../app.js"></script>
<script src="../node_modules/bootstrap/dist/js/bootstrap.min.js>"></script>

在 app.js 内部,我声明了一个 angularjs 模块,因此我可以从我的 index.html 文件中打印插值结果。

包.json:

{
  "name": "restaurant",
  "version": "1.0.0",
  "description": "An AngularJS app for Restaurants",
  "author": "",
  "license": "MIT",
  "devDependencies": {
    "http-server": "^0.11.1"
  },
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "http-server -a localhost -p 4000 ./app"
  },
  "dependencies": {
    "angular": "^1.7.8",
    "bootstrap": "^4.3.1"
  }
}

当我运行 npm start 并转到 localhost:4000/index.html 它说

GET http://localhost:4000/node_modules/bootstrap/dist/css/bootstrap.min.css net::ERR_ABORTED 404 (Not Found)
index.html:4

GET http://localhost:4000/node_modules/angular/angular.min.js net::ERR_ABORTED 404 (Not Found)
app.js:1 Uncaught ReferenceError: angular is not defined
    at app.js:1
(anonymous) @ app.js:1
index.html:6 GET

http://localhost:4000/node_modules/bootstrap/dist/js/bootstrap.min.js%3E net::ERR_ABORTED 404 (Not Found)

我的问题是我是否必须在我的包中编写一些命令才能将 angularjs 和其他库文件复制到我的应用程序目录中的其他文件夹中?

如果不是,为什么 localhost:4000/index.html 找不到这些文件?

TIA 适用于任何解决方案。

标签: node.jsangularjsnpmnode-modules

解决方案


您要求 Web 服务器为目录下的所有内容提供服务app。因此,在 Web 服务器上,目录中的app目录。因此,唯一可以提供的两个文件是/index.html(服务器在其根目录中找到:)app/app.js(它也在其根目录中找到app)。

根,顾名思义,就是根。根之上没有任何东西,否则它就不是根。因此,询问您../anything何时已经处于根源是没有意义的。

旁注:AngularJS 基本上是一个官方废弃的框架。它仍在维护(仅安全错误修复,请参阅文档)几个月,然后就完成了。你为什么要学习一个废弃的框架?


推荐阅读