Lệnh thay brew nodejs trong window
IntroductionThis tutorial will have you deploying a Node.js app to Heroku in minutes. Show Hang on for a few more minutes to learn how to get the most out of the Heroku platform. The tutorial assumes that you have a free Heroku account, and that you have Node.js and npm installed locally. Starting November 28, 2022, free Heroku Dynos, free Heroku Postgres, and free Heroku Data for Redis® plans will no longer be available. If you have apps using any of these resources, you must upgrade to paid plans by this date to ensure your apps continue to run and retain your data. Eligible students can apply for platform credits through our new Heroku for GitHub Students program. See our blog and FAQ for more info. Set upThe Heroku CLI requires Git, the popular version control system. If you don’t already have Git installed, complete the following before proceeding:
In this step you’ll install the Heroku Command Line Interface (CLI). You use the CLI to manage and scale your applications, provision add-ons, view your application logs, and run your application locally. Download and run the installer for your platform: macOS
When installation completes, you can use the On Windows, start the Command Prompt (cmd.exe) or Powershell to access the command shell. Use the
This command opens your web browser to the Heroku login page. If your browser is already logged in to Heroku, simply click the This authentication is required for both the Before you continue, check that you have the prerequisites installed properly. Type each command below and make sure it displays the version you have installed. (Your versions might be different from the example.) If no version is returned, go back to the introduction of this tutorial and install the prerequisites. All of the following local setup will be required to complete the “Declare app dependencies” and subsequent steps. This tutorial will work for any version of Node greater than 10 - check that it’s there:
Now check that you have
Prepare the appIn this step, you will prepare a sample application that’s ready to be deployed to Heroku. If you are new to Heroku, it is recommended that you complete this tutorial using the Heroku-provided sample application. However, if you have your own existing application that you want to deploy instead, see this article to learn how to prepare it for Heroku deployment. To clone a local version of the sample application that you can then deploy to Heroku, execute the following commands in your local command shell or terminal:
You now have a functioning Git repository that contains a simple application as well as a
Deploy the appIn this step you will deploy the app to Heroku. Create an app on Heroku, which prepares Heroku to receive your source code.
When you create an app, a git remote (called Heroku generates a random name (in this case Now deploy your code:
The application is now deployed. Ensure that at least one instance of the app is running:
Now visit the app at the URL generated by its app name. As a handy shortcut, you can open the website as follows:
View logsHeroku treats logs as streams of time-ordered events aggregated from the output streams of all your app and Heroku components, providing a single channel for all of the events. View information about your running app using one of the logging commands,
Visit your application in the browser again, and you’ll see another log message generated. Press Define a ProcfileUse a Procfile, a text file in the root directory of your application, to explicitly declare what command should be executed to start your app. The
This declares a single process type, Procfiles can contain additional process types. For example, you might declare one for a background worker process that processes items off of a queue. Scale the appRight now, your app is
running on a single web dyno. Think of a dyno as a lightweight container that runs the command specified in the You can check how many dynos are running using the
By default, your app is deployed on a free dyno. Free dynos will sleep after a half hour of inactivity (if they don’t receive any traffic). This causes a delay of a few seconds for the first request upon waking. Subsequent requests will perform normally. Free dynos also consume from a monthly, account-level quota of free dyno hours - as long as the quota is not exhausted, all free apps can continue to run. To avoid dyno sleeping, you can upgrade to a hobby or professional dyno type as described in the Dyno Types article. For example, if you migrate your app to a professional dyno, you can easily scale it by running a command telling Heroku to execute a specific number of dynos, each running your web process type. Scaling an application on Heroku is equivalent to changing the number of dynos that are running. Scale the number of web dynos to zero:
Access the app again by hitting refresh on the web tab, or Scale it up again:
For abuse prevention, scaling a non-free application to more than one dyno requires account verification. Declare app dependenciesHeroku recognizes an app as Node.js by the existence of a The demo app you deployed already has a
The Run this command in your local directory to install the dependencies, preparing your system for running the app locally:
Once dependencies are
installed, you will be ready to run your app locally. You’ll notice that a When an app is deployed, Heroku reads the Run the app locallyNow start your application locally using the
Just like Heroku, Open http://localhost:5000 with your web browser. You should see your app running locally. To stop the app from running locally, in the CLI, press Push local changesIn this step you’ll learn how to propagate a local change to the application through to Heroku. As an example, you’ll modify the application to add an additional dependency and the code to use it. Begin by adding a dependency for
Modify
Now test locally:
Visiting your application at http://localhost:5000/cool, you should see cute faces displayed on each refresh: Now deploy. Almost every deploy to Heroku follows this same pattern. First, add the modified files to the local git repository:
Now commit the changes to the repository:
Now deploy, just as you did previously:
Finally, check that everything is working:
You should see another face. Provision add-onsAdd-ons are third-party cloud services that provide out-of-the-box additional services for your application, from persistence through logging to monitoring and more. By default, Heroku stores 1500 lines of logs from your application. However, it makes the full log stream available as a service - and several add-on providers have written logging services that provide things such as log persistence, search, and email and SMS alerts. In this step you will provision one of these logging add-ons, Papertrail. Provision the papertrail logging add-on:
To help with abuse prevention, provisioning an add-on requires account verification. If your account has not been verified, you will be directed to visit the verification site. The add-on is now deployed and configured for your application. You can list add-ons for your app like so:
To see this particular add-on in action, visit your application’s Heroku URL a few times. Each visit will generate more log messages, which should now get routed to the papertrail add-on. Visit the papertrail console to see the log messages:
Your browser will open up a Papertrail web console, showing the latest log events. The interface lets you search and set up alerts: Start a consoleTo get a real feel for how dynos work, you can
create another one-off dyno and run the
If you receive an error, Don’t forget to type Define config varsHeroku lets you externalize configuration - storing data such as encryption keys or external resource addresses in config vars. At runtime, config vars are exposed as environment variables to the application. For example, modify
At the end of the file, add the following definition for the new function,
If you run the app with To set the config var on Heroku, execute the following:
View the config vars that are set using
Deploy your changed application to Heroku and then visit it by running Provision a databaseThe add-on marketplace has a large number of data stores, from Redis and MongoDB providers, to Postgres and MySQL. In this step, you will add a free Heroku Postgres Starter Tier dev database to your app. Add the database:
This creates a database, and sets a Use
Now edit your
Now add another route,
This ensures that when you access your app using the Deploy
this to Heroku. If you access
Now when you access your app’s Read more about Heroku PostgreSQL. A similar technique can be used to install MongoDB or Redis add-ons. Next stepsYou now know how to deploy an app, change its configuration, view logs, scale, and attach add-ons. Here’s some recommended reading. The first, an article, will give you a firmer understanding of the basics. The second is a pointer to the main Node.js category here on Dev Center:
|