Nodejs Express不加载脚本文件
我试图通过节点快递运行一个angular度的应用程序。
1文件结构
AngularNode public /core.js /index.html project.json server.js
2 server.js
var express = require('express'); var app = express(); app.get('*', function(req, res) { res.sendfile('./public/index.html'); // load the single view file (angular will handle the page changes on the front-end) }); // listen (start app with node server.js) ====================================== app.listen(8000); console.log("App listening on port 8000");
3 index.html
<html> <head> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.5/angular.min.js"></script> <script src="./core.js"></script> ........
4 core.js
angular.module('MySystem',[]) .controller('AppController',['$scope',function($scope){ $scope.Name ="Testing Text"; }]);
当我试图运行这个应用程序使用node server.js
这个, index.html
文件得到正确加载,但是这不是检测或加载core.js
文件。 我正在收到以下错误
Uncaught SyntaxError: Unexpected token < core.js:1 Uncaught Error: [$injector:modulerr] http://errors.angularjs.org/1.4.5/$injector/modulerr?p0=MySystem&p1=Error%3…ogleapis.com%2Fajax%2Flibs%2Fangularjs%2F1.4.5%2Fangular.min.js%3A19%3A381) angular.js:38
现在,当我直接从资源pipe理器打开index.html
文件,这是工作或相同的代码我从core.js移动到embedded式HTML,在<head>
与<script>
块正在工作。
我不确定,为什么core.js
在运行节点时没有检测或加载。
经过less许修改就得到了解决scheme
1在server.js
添加了一行app.use(express.static(__dirname + '/public'));
之后线var app = express();
2来自res.sendfile('./public/index.html');
函数更正( res.sendfile('./public/index.html');
res.sendFile('./public/index.html');
现在我可以看到core.js
被检测到并正常工作。
app.get('*', function(req, res) {
是所有get
请求的catch all规则,并且也会匹配./core.js
请求,所以对于./core.js
你的快速应用程序也会发送html文件。
你应该使用express.static来代替:
var express = require('express'); var app = express(); app.use(express.static('./public'));
- Angular没有从承诺的FS.readFile中获取数据
- 我们可以从Visual Studio任务运行器资源pipe理器中运行npm start
- 在制作POST请求时返回空白PDF,但使用GET工作正常
- 学习使用Angularjs和Node进行编码 – 创build基本的联系表单 – 无效的速记属性初始值设定项
- 我正在得到问题phantomjs和业力
- 从咕噜哟angular度的完整的项目select不同的环境?
- angular2-webpack-starter如何将NodeJS服务器添加到项目中
- 节点和angularcookiespath
- 在Angular 2应用程序中无法更改lite-server的基础文件夹