Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Expressjs GET request on javascript files as text/html

I have an application mostly in Angular, and I want to hook it up to Express to encrypt and send private API keys so they won't be stored plainly on the client.

My problem is that the browser reads statically served js files as text/html, which is causing my javascript to not load. You can see that the response is 200 and the file is there, just not being interpreted correctly.
img

index.html has many script requests like these

<script type="text/javascript" src="/keys.js"></script>
<script type="text/javascript" src="/public/lib/underscore/underscore-min.js"></script>
<script type="text/javascript" src="/public/lib/jquery/dist/jquery.min.js"></script>
<script type="text/javascript"  src="/public/lib/bootstrap/dist/js/bootstrap.min.js"></script>
...

Express routing code:

var express = require('express');
var path = require('path');

var app = express();

app.use(express.static(path.resolve('./public')));

app.get('*', function(req,res) {
    res.sendFile(path.resolve('./public/views/index.html'));
});

app.listen(3000);

Anyone experienced with express - what is the proper way to serve static files with different MIME types? I eventually need to serve text/css types as well.

like image 963
ShaharZ Avatar asked Mar 16 '23 03:03

ShaharZ


2 Answers

You've configured your application to return index.html for every request:

app.get('*', function(req,res) {
    res.sendFile(path.resolve('./public/views/index.html'));
});

So express dutifully does just that, serves index.html for any and all requests, including the requests that you want to have return the js files via your script tags. For example, a request to /public/lib/underscore/underscore-min.js will actually return the file at /public/views/index.html.

A simple fix would be to just return index.html for a root request:

app.get('/', function(req,res) {
    res.sendFile(path.resolve('./public/views/index.html'));
});

In this way your index.html would be served at the root but your javascript assets could still be reached because you aren't serving index.html for every request.

Additionally, since you've told express that static assets can be found at /public, there's no need to include that directory when requesting them. So, your script includes should look like:

<script type="text/javascript" src="/lib/underscore/underscore-min.js"></script>
<script type="text/javascript" src="/lib/jquery/dist/jquery.min.js"></script>
<script type="text/javascript"  src="/lib/bootstrap/dist/js/bootstrap.min.js"></script>
like image 148
Alex Avatar answered Mar 24 '23 06:03

Alex


I think this is your problem:

app.get('*', function(req,res) {
    res.sendFile(path.resolve('./public/views/index.html'));
});

You're sending index.html for literally every request.

like image 42
Rob Johansen Avatar answered Mar 24 '23 06:03

Rob Johansen