In my previous articles, I showed you how to deploy your static website to Heroku. So in this article, I am showing you how to deploy a Angular App to Heroku for free.
What is Heroku is?
In my previous article, I have duscussed what is Heroku is? You can refer it to get clear idea about Heroku and how you configure Heroku in your local environment
Create new Angular project
First upon we need to Create a new angular project through Angular CLI.
1
ng new heroku-deploy
After successfully creating your Angular project you need to navigate your app directory heroku-deploy” folder by typing the following command in the terminal:
1
cd heroku-deploy
Add express path
We need something to serve our files. Let’s go with express. We will also need “path” to setup our server. To add "express path" to our project, use following command:
1
npm install --save express path
In your main application directory create a file called server.js and add the following code:
// Send all requests to index.html app.get('/*', function(req, res) { res.sendFile(path.join(__dirname + '/dist/MY_APP_NAME_HERE/index.html')); });
// default Heroku PORT app.listen(process.env.PORT || 3000);
Replace “MY_APP_NAME_HERE” to the name of your app.
Configure package.json
Open your package.json file and change the “start”: “ng serve” in the “scripts: ” {….} section to “start”: “node server.js” this command will run the server.js file through the server.
Under scripts: section of your package.json file, put below code:
To push your app to Heroku, you should have the Heroku cli installed in your local machine.
If not please follow the instruction given here.
To deploy your application to Heroku, you should now folow below command in your terminal:
1 2 3 4 5 6 7 8 9
git init
git add .
git commit -m "my first commit'
heroku create
git push heroku master
Now you have succefully deploy your Angular app to heroku and you can see the deploy link in your terminal. Open it up and you should see your site.
If you have any issues feel free to leave a message in the comments section.