node.js - Expressjs GET request on javascript files as text/html -


i have application in angular, , want hook express encrypt , send private api keys won't stored plainly on client.

my problem browser reads statically served js files text/html, causing javascript not load. can see response 200 , file there, not being interpreted correctly.
img

index.html has many script requests 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 express - proper way serve static files different mime types? need serve text/css types well.

you've configured application return index.html every request:

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

so express dutifully that, serves index.html , requests, including requests want have return js files via script tags. example, request /public/lib/underscore/underscore-min.js return file @ /public/views/index.html.

a simple fix return index.html root request:

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

in way index.html served @ root javascript assets still reached because aren't serving index.html every request.

additionally, since you've told express static assets can found @ /public, there's no need include directory when requesting them. so, script includes should 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> 

Comments