简体   繁体   English

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

[英]Errors in the code after build using angular cli (ng build)

I am new angular, trying to set up new project in angular, using angular/cli. 我是新角度用户,尝试使用angular / cli建立新角度项目。 I did following steps to setup a express server and angular as the front end. 我按照以下步骤设置了快速服务器,并将angular作为前端。

installed angular/cli; 安装角度/ cli; then ng new testApp; 然后ng new testApp; ng build; ng build; this created a dist folder like dist>testApp>and all the files(index,html, main.js. . . . so on), 这创建了一个dist文件夹,例如dist> testApp>和所有文件(index,html,main.js ...等),

and i have installed express and body-parser. 并且我已经安装了express和body-parser。 now in my server.js i have following code. 现在在我的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}`));

Now when i started the server and tried " http://localhost:3000/ " on my browser I see errors in console saying unexpected token in all .js files like main.js, polyfills.js so when i look at the sources tab i can see even the js files are having the same index.html content in them. 现在,当我启动服务器并在浏览器上尝试“ http:// localhost:3000 / ”时,我在控制台中看到错误,在诸如main.js,polyfills.js的所有.js文件中说出意外的令牌,因此当我查看“源”选项卡时我什至可以看到js文件中都包含相同的index.html内容。

So can any tell me what i was missing or doing the wrong way. 因此,有谁能告诉我我在想什么或做错了什么。 please. 请。 Thanks. 谢谢。

Just rewrite your server.js file as below. 只需如下重写您的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}`));

This may solve your issue. 这样可以解决您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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