how to deploy angular2 app built using angular-cli

后端 未结 9 691
梦谈多话
梦谈多话 2021-01-30 07:12

I have created n new angular app using angular-cli.

I completed the app and preview it using ng-serve, it is working perfectly.

After that I used ng build --

9条回答
  •  既然无缘
    2021-01-30 07:54

    Here's an example with Heroku:

    1. Create a Heroku account and install the CLI

    2. Move the angular-cli dep to the dependencies in package.json (so that it gets installed when you push to Heroku.

    3. Add a postinstall script that will run ng build when the code gets pushed to Heroku. Also add a start command for a Node server that will be created in the following step. This will place the static files for the app in a dist directory on the server and start the app afterward.

    "scripts": {
      // ...
      "start": "node server.js",
      "postinstall": "ng build --aot -prod"
    }
    
    1. Create an Express server to serve the app.
    // server.js
    const express = require('express');
    const app = express();
    // Run the app by serving the static files
    // in the dist directory
    app.use(express.static(__dirname + '/dist'));
    // Start the app by listening on the default
    // Heroku port
    app.listen(process.env.PORT || 8080);
    
    1. Create a Heroku remote and push to depoy the app.
    heroku create
    git add .
    git commit -m "first deploy"
    git push heroku master
    

    Here's a quick writeup I did that has more detail, including how to force requests to use HTTPS and how to handle PathLocationStrategy :)

提交回复
热议问题