i'm testing express 4.x with angular.js and i got some problems , someone got an error like this?
Console Chrome:
Resource interpreted as Script but transferred with MIME type text/html: "http://localhost:8080/javascript/main.js". localhost/:5
Uncaught SyntaxError: Unexpected token < main.js:1
Uncaught Error: [$injector:modulerr] http://errors.angularjs.org/1.2.17/$injector/modulerr?p0=appTmo&p1=Error%3A…gleapis.com%2Fajax%2Flibs%2Fangularjs%2F1.2.17%2Fangular.min.js%3A18%3A203)
My app is very simple:
server.js:
var express = require('express');
var app = express();
var router = express.Router();
var morgan = require('morgan');
app.use(router);
app.use(morgan('dev'));
app.use(express.static(__dirname+'/public'));
router.get('*',function (req,res) {
res.sendfile('public/index.html');
});
app.listen(8080);
index.html:
<html ng-app='app'>
<head>
<title>App</title>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.17/angular.min.js"></script>
<script type="text/javascript" src="javascript/main.js"></script>
</head>
<body >
<h1>test</h1>
</body>
</html>
main.js:
var app = angular.module('app',[]);
Move the app.use(router);
below the app.use(express.static(__dirname+'/public'));
. So your server.js should instead like:
var express = require('express');
var app = express();
var router = express.Router();
var morgan = require('morgan');
app.use(morgan('dev'));
app.use(express.static(__dirname+'/public'));
app.use(router);
router.get('*',function (req,res) {
res.sendfile('public/index.html');
});
app.listen(8080);
When you call app.use(router)
prior to the static call, the router gets called first on requests. The *
matches all, so it'll process all incoming requests. The problem you're seeing is that the index.html
is being returned for the request for main.js
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.