如何通过npm安装

时间:2018-02-15 13:44:09

标签: jquery node.js express npm bootstrap-4

enter image description here我正在研究一个学习node.js的简单项目,我尝试通过npm安装bootstrap。 bootstrap.css似乎正在工作,但页面上的输出很差,还下载了Jquery和tether,但是当我检查网页的网络部分时,bootstrap.js和jquery.js文件没有加载。

我已使用express.static将目录设置为静态,但文件仍未加载。

app.use(express.static(path.join(__dirname, 'public')));
app.use('/css', express.static(__dirname+'/node_modules/bootstrap/dist/css'));
app.use('/js', express.static(__dirname+'/node_modules/bootstrap/dist/js'));
app.use('/js', express.static(__dirname+'/node_modules/tether/dist/js'));
app.use('/js', express.static(__dirname+'/node_modules/jquery/dist'));


    <script src="/js/jquery.min.js"></script>
    <script src="/js/bootstrap.min.js"></script>
    <script src="/js/tether.min.js"></script>
</body>

<%- include('includes/header') %>
<nav class="navbar navbar-toggleable-md navbar-light navbar-inverse">
  <button class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
    <span class="navbar-toggler-icon"></span>
  </button>
  <a class="navbar-brand" href="#">PassportApp</a>
  <div class="collapse navbar-collapse" id="navbarNav">
    <ul class="navbar-nav">
      <li class="nav-item active">
        <a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="#">Features</a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="#">Pricing</a>
      </li>
      <li class="nav-item">
        <a class="nav-link disabled" href="#">Disabled</a>
      </li>
    </ul>
  </div>
</nav>
<div class="container">

  <div class="starter-template">
    <h1>Members Area</h1>
    <p> Welcome to the members area.</p>
  </div>

</div><!-- /.container -->

<%- include('includes/footer') %>

2 个答案:

答案 0 :(得分:1)

首先,Bootstrap 4中已经存在NO tether.js(不再)了。

这意味着您正在尝试加载过时的版本的Bootstrap。

其次,加载JS文件的顺序很重要。在您的情况下,您在tether.js之后加载bootstrap.min.js,这绝对不是正确的方法。{1}}

这里有一个方便的HTML模板,包含所有正确的文件和正确的顺序(重新安装Bootstrap并确保您的安装输出那种文件):

&#13;
&#13;
<!doctype html>
<html lang="en">
  <head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">

    <title>Hello, world!</title>
  </head>
  <body>
    <h1>Hello, world!</h1>

    <!-- Optional JavaScript -->
    <!-- jQuery first, then Popper.js, then Bootstrap JS -->
    <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
  </body>
</html>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

您可以添加自定义脚本,例如additionalScript.js。

<!DOCTYPE html>
<html>
    <head>
        <title>New App</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <link rel="shortcut icon" href="assets/images/favico.ico" />
        <script src="assets/js/additionalScript.js" ></script>
    </head>
    <body class="skin-light-blue" style="display:none">

    </body>
</html>

因此,您无需在js之前添加额外的'/'。