繁体   English   中英

使用angular cli(ng build)构建后的代码错误

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM