Express.js以文本方式继续提供Angular2 index.html
我似乎无法使用Express.js服务我的前端Angular2应用程序。 它只是保持服务index.html作为文本和脚本似乎不加载。
重现步骤:
npm install angular-cli -g ng new ng2app && cd ng2app npm install express --save && npm install touch src/server.js
更新server.js文件
// src/server.js var express = require('express'); var app = express(); app.set('port', (process.env.PORT || 5000)); app.get('/', function(req, res) { res.sendFile(__dirname + '/index.html') }) app.listen(app.get('port'), function() { console.log('Node app is running on port', app.get('port')); });
运行服务器
node src/server.js # => Node app is running on port 5000
HTTP://本地主机:5000 /
问题
浏览器在页面上吐出这些东西,从不做任何事情(即,它不会加载我的前端angular2应用程序)
{{#unile environment.production}} {{/ unless}}正在加载… {{#each scripts.polyfills}} {{/ each}}
也在这个页面上有以下的浏览器控制台错误
GET http:// localhost:5000 / ember-cli-live-reload.js
(index):19 GET http:// localhost:5000 /%7B%7B。%7D%7D
(索引):21未捕获ReferenceError:系统未定义
我究竟做错了什么? PS有棱angular的前端部分没有问题。 我知道这是因为做npm start
工作和ng2app加载就好像它应该罚款。
这是我从一个种子项目中获得的最小值: https : //github.com/spboyer/quickstart-ng-cli
var express = require('express'), path = require('path'), fs = require('fs'); var app = express(); var staticRoot = __dirname + '/'; app.set('port', (process.env.PORT || 3000)); app.use(express.static(staticRoot)); app.use(function(req, res, next){ // if the request is not html then move along var accept = req.accepts('html', 'json', 'xml'); if(accept !== 'html'){ return next(); } // if the request has a '.' assume that it's for a file, move along var ext = path.extname(req.path); if (ext !== ''){ return next(); } fs.createReadStream(staticRoot + 'index.html').pipe(res); }); app.listen(app.get('port'), function() { console.log('app running on port', app.get('port')); });
对我来说,它看起来像你没有实际上跑angularcli构build。