Home > database >  Bootstrap does not load in HTML file in node js app
Bootstrap does not load in HTML file in node js app

Time:09-17

I'm new with Node js and basically, this is my first app in Node js and javascript. I have the following HTML file and try to load bootstrap CSS from node modules

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>MyApp</title>
    <link  href='../node_modules/bootstrap/dist/css/bootstrap.css' rel ="stylesheet" media="all">

<body>
  <nav class="navbar navbar-expand-lg navbar-light bg-light">
    <a class="navbar-brand" href="#">Navbar</a>
    <button class="navbar-toggler" 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>
    <div class="collapse navbar-collapse" id="navbarNav">
      <ul class="navbar-nav">
        <li class="nav-item active">
          <a class="nav-link" href="/api/images">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>
</body>

</html>

I have tried :

<link  href='../node_modules/bootstrap/dist/css/bootstrap.css' media="all">

and

<link  href='../node_modules/bootstrap/dist/css/bootstrap.css'>

and

<link  href='../node_modules/bootstrap/dist/css/bootstrap.min.css' rel ="stylesheet" media="all">

no success though.

index.html is in the view folder and index.js is in the root folder.

bootstrap does not apply to the HTML file?

CodePudding user response:

You could use a static folder, then link it in html file like this:

app.use('/css', express.static(__dirname   '/node_modules/bootstrap/dist/css')); // redirect CSS bootstrap

then add the link like:

<link rel="stylesheet" href="/css/bootstrap.min.css">

CodePudding user response:

Try loading bootstrap from the website

<!DOCTYPE html>

<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>MyApp</title>
    <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-KyZXEAg3QhqLMpG8r 8fhAXLRk2vvoC2f3B09zVXn8CA5QIVfZOJ3BCsw2P0p/We" crossorigin="anonymous">

<body>
  <nav class="navbar navbar-expand-lg navbar-light bg-light">
    <a class="navbar-brand" href="#">Navbar</a>
    <button class="navbar-toggler" 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>
    <div class="collapse navbar-collapse" id="navbarNav">
      <ul class="navbar-nav">
        <li class="nav-item active">
          <a class="nav-link" href="/api/images">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>
</body>

</html>

It should work

  • Related