简体   繁体   中英

How to host angular 7 website with node js for server side

I am a beginner in front end development. I have created a website with angular 7 and implemented contact form(send email) using node js for server side. Angular and node js are two different apps in my project. Now I want to host it on GoDaddy server, but I am not getting the way for doing it. Please help me to understand how to do it.

I would do something like this,

const express = require('express');
const cors = require('cors');
const path = require('path');

const app = express();

app.use(cors());
app.use(express.static(path.join(__dirname, './public')));

app.get('/', (req, res) => {
    res.sendFile(path.join(__dirname, './dist/index.html'));
});

app.listen(8080, () => {
    console.log('Listening to port 8080');

});

where dist folder is the static build of your Angular project

Using Express, we can serve the static files through GET request,

however, your URL will looks like this

http://hostname:8080

to eliminate port 8080 from the URL, we can use the default (port 80) to redirect it to port 8080.

We can do this by using the following command

sudo iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-ports 8080

Hope this helps ;)

Create static development build using the command Ng build --prod it will create a dist folder. Dist folder will contain the production static build. You can deploy it anywhere shutout the use of node environment. You can even you amazon S3 bucket for deployment. For more information you can go through the following link. https://angular.io/guide/deployment

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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