MEAN Stack:当 Angular 在 Node 文件夹中时,如何从 Node 启动 Angular
MEAN Stack: How do you start Angular from Node, when Angular is inside Node folder
我有一个 MEAN 堆栈应用程序,我将 Node 安装在一个名为 'myApp' 的文件夹中。而且,我的 Angular 安装在 'myApp' 文件夹中,名为 'Angular-src'。这是基础结构。因此,它在我的本地机器上完美运行。对于 Node,我在命令 window 上使用 'nodemon',在另一个命令 window.
上使用 'ng serve' 作为 Angular
但是,当我在 Heroku 上托管它时,我开始遇到问题。我按照说明为 Prod 构建 Angular 应用程序,连接 MongoDB,部署它,当在浏览器中查看时,它表明我的节点服务器 运行 正常。但是没有任何 Angular 位 运行 的迹象。我在网上看了很多,但没有明确的答案。
我认为这与我的 'package.json' 文件有关,在 "scripts" 部分。我有两个 'package.json' 个文件;一个在 Node 的 'myApp' 文件夹中,另一个在 'Angular-src' 文件夹中。这些是详细信息:
(1) 'package.json' 在节点文件夹中 -
{
"name": "-----",
"version": "0.0.0",
"description": "-----",
"main": "app.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node app"
},
"dependencies": {
"express": "~4.17.1",
"mongoose": "~5.6.0",
"cors": "~2.8.5",
"body-parser": "~1.19.0"
},
"author": "Harsha W. & Sujeewa B.",
"license": "ISC"
}
(2) 'package.json' in angular-src 文件夹(长文件被截断)-
{
"name": "angular-src",
"version": "0.0.0",
"scripts": {
"ng": "ng",
"start": "node server.js",
"build": "ng build",
"test": "ng test",
"lint": "ng lint",
"heroku-postbuild": "ng build --prod",
"e2e": "ng e2e"
},
"private": true,
"dependencies": { ...
(3)我的app.js在Node文件夹-
const express = require('express');
const mongoose = require('mongoose');
const cors = require('cors');
const bodyParser = require('body-parser');
const dbConfig = require('./config/database');
const http = require('http');
const fs = require('fs');
// Connecting with mongo db
mongoose.Promise = global.Promise;
mongoose.connect(dbConfig.database, {
useNewUrlParser: true
}).then(() => {
console.log('Database sucessfully connected')
},
error => {
console.log('Database could not connected: ' + error)
}
)
mongoose.set('useFindAndModify', false);
// Setting up port with express js
const coursesRoute = require('./controllers/courses.route');
/** other routes I have removed to unclutter the code **/
const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: false
}));
app.use(cors())
app.use('/api/course', coursesRoute);
app.get('/', (req, res) => {
res.send("Hello HTTPS!")
})
// port
const port = process.env.PORT || 3000;
http.createServer(app).listen( port, () => {
console.log('Listening on port ' + port);
});
**
我需要知道的是当我的文件夹结构如上时如何从我的节点提供服务Angular。
我认为您正在将源代码部署到 heroku 而不是构建版本。现在您的 angular
服务器 运行 在 4200
上,因为它正在开发中,但是一旦您在 Heroku
上部署它。您将只有一个端口可以使用(由 heroku 分配)。
构建 angular 应用程序的命令是 ng build
,但不要只是去 运行 这个命令(了解它)。这将获取所有组件并在一个类似于 HTML files
的文件夹中构建几个文件,然后您可以部署这些文件。
了解 Deploying Angular App。 youtube 上有各种教程,您可以在其中找到如何在云上部署 MEAN 堆栈应用程序。
我有一个 MEAN 堆栈应用程序,我将 Node 安装在一个名为 'myApp' 的文件夹中。而且,我的 Angular 安装在 'myApp' 文件夹中,名为 'Angular-src'。这是基础结构。因此,它在我的本地机器上完美运行。对于 Node,我在命令 window 上使用 'nodemon',在另一个命令 window.
上使用 'ng serve' 作为 Angular但是,当我在 Heroku 上托管它时,我开始遇到问题。我按照说明为 Prod 构建 Angular 应用程序,连接 MongoDB,部署它,当在浏览器中查看时,它表明我的节点服务器 运行 正常。但是没有任何 Angular 位 运行 的迹象。我在网上看了很多,但没有明确的答案。
我认为这与我的 'package.json' 文件有关,在 "scripts" 部分。我有两个 'package.json' 个文件;一个在 Node 的 'myApp' 文件夹中,另一个在 'Angular-src' 文件夹中。这些是详细信息:
(1) 'package.json' 在节点文件夹中 -
{
"name": "-----",
"version": "0.0.0",
"description": "-----",
"main": "app.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node app"
},
"dependencies": {
"express": "~4.17.1",
"mongoose": "~5.6.0",
"cors": "~2.8.5",
"body-parser": "~1.19.0"
},
"author": "Harsha W. & Sujeewa B.",
"license": "ISC"
}
(2) 'package.json' in angular-src 文件夹(长文件被截断)-
{
"name": "angular-src",
"version": "0.0.0",
"scripts": {
"ng": "ng",
"start": "node server.js",
"build": "ng build",
"test": "ng test",
"lint": "ng lint",
"heroku-postbuild": "ng build --prod",
"e2e": "ng e2e"
},
"private": true,
"dependencies": { ...
(3)我的app.js在Node文件夹-
const express = require('express');
const mongoose = require('mongoose');
const cors = require('cors');
const bodyParser = require('body-parser');
const dbConfig = require('./config/database');
const http = require('http');
const fs = require('fs');
// Connecting with mongo db
mongoose.Promise = global.Promise;
mongoose.connect(dbConfig.database, {
useNewUrlParser: true
}).then(() => {
console.log('Database sucessfully connected')
},
error => {
console.log('Database could not connected: ' + error)
}
)
mongoose.set('useFindAndModify', false);
// Setting up port with express js
const coursesRoute = require('./controllers/courses.route');
/** other routes I have removed to unclutter the code **/
const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: false
}));
app.use(cors())
app.use('/api/course', coursesRoute);
app.get('/', (req, res) => {
res.send("Hello HTTPS!")
})
// port
const port = process.env.PORT || 3000;
http.createServer(app).listen( port, () => {
console.log('Listening on port ' + port);
});
** 我需要知道的是当我的文件夹结构如上时如何从我的节点提供服务Angular。
我认为您正在将源代码部署到 heroku 而不是构建版本。现在您的 angular
服务器 运行 在 4200
上,因为它正在开发中,但是一旦您在 Heroku
上部署它。您将只有一个端口可以使用(由 heroku 分配)。
构建 angular 应用程序的命令是 ng build
,但不要只是去 运行 这个命令(了解它)。这将获取所有组件并在一个类似于 HTML files
的文件夹中构建几个文件,然后您可以部署这些文件。
了解 Deploying Angular App。 youtube 上有各种教程,您可以在其中找到如何在云上部署 MEAN 堆栈应用程序。