我正在使用Angular 6构建应用程序,现在我想使用ExpressJS为其提供服务。
为此,我继续使用ng serve --prod
编译应用程序,但是由于正在控制台中获取SyntaxError: expected expression, got '<'
,因此应用程序未加载。
我以为我的应用程序可能出了点问题,但是在ng serve
上运行正常,因此我在ng serve --prod
上进行了尝试,一切仍然正常运行。
然后,我决定仅尝试ng build
(不尝试--prod
),并遇到相同的错误。
在GitHub问题中,我找到了一些相关的东西,但特定于某些软件包和较旧版本的angular。
如果相关,这是我的package.json:
{
"name": "mt",
"version": "0.0.0",
"scripts": {
"ng": "ng",
"start": "ng serve",
"build": "ng build",
"test": "ng test",
"lint": "ng lint",
"e2e": "ng e2e"
},
"private": true,
"dependencies": {
"@angular/animations": "^6.1.0",
"@angular/cdk": "^6.4.7",
"@angular/common": "^6.1.0",
"@angular/compiler": "^6.1.0",
"@angular/core": "^6.1.0",
"@angular/forms": "^6.1.0",
"@angular/http": "^6.1.0",
"@angular/material": "^6.4.7",
"@angular/platform-browser": "^6.1.0",
"@angular/platform-browser-dynamic": "^6.1.0",
"@angular/router": "^6.1.0",
"core-js": "^2.5.4",
"d3": "^5.7.0",
"dateformat": "^3.0.3",
"rxjs": "~6.2.0",
"xlsx": "^0.14.1"
},
"devDependencies": {
"@angular-devkit/build-angular": "~0.8.0",
"@angular/cli": "~6.2.1",
"@angular/compiler-cli": "^6.1.0",
"@angular/language-service": "^6.1.0",
"@types/jasmine": "~2.8.8",
"@types/jasminewd2": "~2.0.3",
"@types/node": "~8.9.4",
"codelyzer": "~4.3.0",
"jasmine-core": "~2.99.1",
"jasmine-spec-reporter": "~4.2.1",
"karma": "~3.0.0",
"karma-chrome-launcher": "~2.2.0",
"karma-coverage-istanbul-reporter": "~2.0.1",
"karma-jasmine": "~1.1.2",
"karma-jasmine-html-reporter": "^0.2.2",
"protractor": "~5.4.0",
"ts-node": "~7.0.0",
"tslint": "~5.11.0",
"typescript": "~2.9.2"
}
}
我想问题不在于代码本身,因为应用程序在使用ng serve
命令(不管是否带有--prod
标志的情况下都可以正常工作)。
关于如何解决此问题的任何想法?
编辑:这是我为该应用服务的快速文件
const html = __dirname + '/dist/';
const port = 4200;
// Express
const bodyParser = require('body-parser');
const compression = require('compression');
const express = require('express');
var app = express();
app
.use(compression())
.use(bodyParser.json())
// Static content
.use(express.static(html))
// Start server
.listen(port, function () {
console.log('Port: ' + port);
console.log('Html: ' + html);
});
app.get('*', function(req, res) {
res.sendfile('./dist/mtloops-web-app/index.html');
});