我在一个名为的文件夹中创建了一个angular应用,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
文件,用于从Node端点渲染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}`));
现在,在运行节点服务器时,我期望节点服务器在加载时呈现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 <
您的服务器不提供文件一样http://localhost:3000/angular/angular-app/polyfills.js
-你需要重定向所有的文件请求dist/
-目前你做只有一个-/angular
到/dist/index.html
在这里您可以获得有关如何执行操作的更多信息:https : //blog.cloudboost.io/run-your-angular-app-on-nodejs-c89f1e99ddd3
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句