[英]Errors in the code after build using angular cli (ng build)
我是新角度用戶,嘗試使用angular / cli建立新角度項目。 我按照以下步驟設置了快速服務器,並將angular作為前端。
安裝角度/ cli; 然后ng new testApp; ng build; 這創建了一個dist文件夾,例如dist> testApp>和所有文件(index,html,main.js ...等),
並且我已經安裝了express和body-parser。 現在在我的server.js中,我有以下代碼。
const express = require('express');
const bodyParser = require('body-parser');
const path = require('path');
const http = require('http');
const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended : false}));
app.use(express.static(path.join(__dirname, 'dist')));
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, 'dist/testApp/index.html'));
});
const port = process.env.port || '3000';
app.set('port', port);
const server = http.createServer(app);
server.listen(port, () => console.log(`listening on port : ${port}`));
現在,當我啟動服務器並在瀏覽器上嘗試“ http:// localhost:3000 / ”時,我在控制台中看到錯誤,在諸如main.js,polyfills.js的所有.js文件中說出意外的令牌,因此當我查看“源”選項卡時我什至可以看到js文件中都包含相同的index.html內容。
因此,有誰能告訴我我在想什么或做錯了什么。 請。 謝謝。
只需如下重寫您的server.js文件。
const express = require('express');
const bodyParser = require('body-parser');
const path = require('path');
const http = require('http');
const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended : false}));
app.use('/', express.static(path.join(__dirname, 'dist', 'testApp')));
const port = process.env.port || '3000';
app.set('port', port);
const server = http.createServer(app);
server.listen(port, () => console.log(`listening on port : ${port}`));
這樣可以解決您的問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.