Я создал угловое приложение в папке с именем angular-app
и успешно создал сборку, используя ng build
. Я протестировал сборку с использованием http-сервера. Я перешел в папку angular-app
и побежал
http-server ./dist
И угловое приложение отображается. Вот страница angular-app/dist/index.html
сборки.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>AngularApp</title>
<base href="/angular-app/">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" type="image/x-icon" href="favicon.ico">
</head>
<body>
<app-root></app-root>
<script type="text/javascript" src="runtime.js"></script><script type="text/javascript" src="polyfills.js"></script><script type="text/javascript" src="styles.js"></script><script type="text/javascript" src="vendor.js"></script><script type="text/javascript" src="main.js"></script></body>
</html>
Теперь внутри папки angular-app
я создал файл server.js
для визуализации сборки Angular с конечной точки узла. Вот как выглядит файл angular-app/server.js
:
const express = require('express');
const path = require('path');
const http = require('http');
const app = express();
// Point static path to dist
app.use(express.static(path.join(__dirname, '/dist')));
// Catch all other routes and return the index file
app.get('/angular', (req, res) => {
console.log(__dirname);
res.sendFile(path.join(__dirname, 'dist/index.html'));
});
/**
* Get port from environment and store in Express.
*/
const port = process.env.PORT || '3000';
app.set('port', port);
/**
* Create HTTP server.
*/
const server = http.createServer(app);
/**
* Listen on provided port, on all network interfaces.
*/
server.listen(port, () => console.log(`API running on localhost:${port}`));
Теперь, когда я запускаю сервер узлов, я ожидаю, что сервер Node будет отображать приложение Angular при загрузке http://localhost:3000//angular
.
Но вместо этого я выдаю следующую ошибку в консоли браузера.
GET http://localhost:3000/angular/angular-app/runtime.js 404 (Not Found)
Refused to execute script from '<URL>' because its MIME type ('text/html') is not executable, and strict MIME type checking is enabled.
angular:13 GET http://localhost:3000/angular/angular-app/polyfills.js 404 (Not Found)
favicon.ico:1 GET http://localhost:3000/angular/angular-app/favicon.ico 404 (Not Found)
Есть мысли о том, что мне не хватает ??
Обновление: Согласно комментарию изменил app.get ('/ angular') на app.get ('*'), но все равно получил следующую ошибку в консоли.
Uncaught SyntaxError: Unexpected token <
polyfills.js:1 Uncaught SyntaxError: Unexpected token <
styles.js:1 Uncaught SyntaxError: Unexpected token <
vendor.js:1 Uncaught SyntaxError: Unexpected token <
main.js:1 Uncaught SyntaxError: Unexpected token <