问题
I have a web application. It's a blog and question-answer website made with MEAN stack making CRUD operations using Mongodb. Before this I have deployed many applications built in react and angular wherein only one server was required i.e. npm start as there was no back-end server code. But this time I decided to keep my back-end code separate from my front-end code. server.js is in a different folder and is running on port:3000, while application is running on localhost:4200. I am using cors for cross origin communication. The project is open source. I have uploaded on github. Here's the repo. Consider it all yours. Please at least give me some hint how to deploy this project. I'm ready to buy domain and VPS also.
PS: Please go through README.md
回答1:
for production build of the angular application you have to use ng build --prod with these command you will get the compiled version of your application inside the dist folder serve this dist folder using nodejs
var express = require("express");
var compression = require('compression')
var port = 7600;
var app = express();
app.use(compression());
app.use("/", express.static(__dirname + '/dist', { etag: true, maxAge: '3d' }));
app.get('*', function (req,res) {
res.sendFile('index.html', {root: __dirname + '/dist' })
});
app.listen(port, function() {
console.log("Express server listening on port " + port);
})
you can use nodejs to create server or expressjs just your port number should be different than your backend server
回答2:
- You have to use
ng build --prodfor compile your development file. - Then you need to setup your node.js file. please check the code
Then just upload all the server files and dist folder to server.
And run the server using
pm2. You have to install it from please check this link- And run just
pm2 start server.js
That's it. Now your fron-end and back-end run together on your specific port.
来源:https://stackoverflow.com/questions/57969049/how-and-where-to-deploy-an-angular-8-web-application-that-needs-two-servers-conc