0

My I'm using express.js as a static server and the code is looking like this:

var express = require('express'),
  app = express();

app
  .use(express.static('./public'))
  .get('*', function (req, res) {
    res.sendfile('public/main.html');
  })
  .listen(3000);

My file structure:

.
├── bower_components
│   ├── angular
│   ├── bootstrap
│   └── jquery
├── node_modules
│   └── express
├── public
│   ├── main.html
│   ├── src
│   └── views
└── server.js

My HTML is looking like this:

<!DOCTYPE html>
<html ng-app="ContactsApp">
<head lang="en">
  <meta charset="UTF-8">
  <title> Contacts </title>
  <base href="/" />
  <link rel="stylesheet" href="src/bootstrap.min.css"/>

</head>
 <body>    
  <script src="../bower_components/jquery/dist/jquery.min.js"></script>
  <script src="../bower_components/bootstrap/dist/js/bootstrap.min.js"></script>
  <script src="../bower_components/angular/angular.min.js"></script>
  <script src="src/app.js"></script>
 </body>
</html>

The Browser is not loading the angular library und I'm getting a error Resource interpreted as Script but transferred with MIME type text/html: "http://localhost:3000/bower_components/angular/angular.min.js"..

What I'm doing wrong? I couldn't find any answers from the previous questions.

0

1 Answer 1

1

This line says that you are serving your static files from ./public

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

So only this tree will be exposed to the public:

   ├── main.html
   ├── src
   └── views

Your bower_components diretory is not in public, thus it is not exposed.

To fix this, you can create a .bowerrc file in the root of your application with the following:

{
  "directory": "public/bower_components/"
}

It will tell to bower to install the bower components in public instead of the default bower_components directory. Just run bower install after creating this file, and don't forget to update your index.html with the updated paths.

EDIT: Dont be tempted to change use(express.static('./public')) by use(express.static('./')) cause it will easily resolve your issue. It will expose your whole directory structure (and all your files) and is not good for obvious security reasons :)

Sign up to request clarification or add additional context in comments.

1 Comment

You are right! The directory was not exposed to the static server.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.