Node.js study notes 10- cloud (heroku) on Node.js applications

The cloud (heroku) on Node.js applications

Read this blog takes about 10 minutes

Learn the basic concepts in front of Node.js development.
But also to understand the most popular method of virtualization technology Docker local deployment.

Today, we try to cloud the Node REST API service. But with a very simple and very sophisticated way, that is heroku. URL: www.heroku.com
Here Insert Picture Description

Heroku supports multiple programming languages ​​is a cloud platform as a service. Salesforce.com was acquired in 2010. As one of the Heroku cloud platform to start, from June 2007 to develop, then it only supports Ruby, but then adds Java, Node.js, Scala, Clojure, Python as well (not recorded in official documents) PHP and support for Perl.

The industry has a lot of cloud platforms (PaaS), SAP also provides the SAP cloud platform, you can enhance enterprise development and innovation capacity. PaaS gives developers a lot of convenient services, it shields the complex operation of IaaS, allowing developers within the shortest possible time to focus on business innovation.

Select Heroku, mainly because it offers some free services, you can put a relatively simple service / app / website fast on the cloud, then it can also compare and SAP cloud platform differences.

Good code is its own best documentation. As you’re about to add a comment, ask yourself, “How can I improve the code so that this comment isn’t needed?” --Steve McConnell

Mainly through the following a few simple steps, you can quickly put a cloud on the service:

  1. Ready to work
  2. Node.js project preparation
  3. Uploaded to GitHub
  4. Heroku command installation and deployment tools
  5. Testing of cloud services

Ready to work

  • Git

Basic Operations need to install git on the computer, and are familiar with git

  • Heroku

We first need to register an account. Recommended by Gmail can be registered with immediate effect.

Node.js project preparation

Borrowing code on a project. It simply returns a user's data through JSON.

Little do some minor adjustments: code changes the port number.

server.js:

const data = require('./data');
const express = require('express');

const PORT = process.env.PORT || 3000;

const app = express();

app.get('/user', (req, res) => {
    res.send(data);
});

app.listen(PORT);

Port number using environment variables, run time is the port number on the cloud server.

Upload to github

Create a project on github, and then upload:

echo "# heroku01" >> README.md
git init
git add README.md
git commit -m "first commit"
git remote add origin https://github.com/rangwei/heroku01.git
git push -u origin master

Heroku command installation and deployment tools

  1. Heroku installed under Mac command tool:
brew install heroku/brew/heroku
  1. Login heroku:
heroku login
  1. Create an application:
heroku create

Soon you can see the created, the output contains the app name, address and application code address:

Creating app... done, ⬢ damp-temple-40624
https://damp-temple-40624.herokuapp.com/ | https://git.heroku.com/damp-temple-40624.git
  1. Synchronization code to heroku:
git push https://git.heroku.com/damp-temple-40624.git

Then automatically upload the code is compiled to run, output is as follows:

Enumerating objects: 8, done.
Counting objects: 100% (8/8), done.
Delta compression using up to 8 threads
Compressing objects: 100% (6/6), done.
Writing objects: 100% (8/8), 5.16 KiB | 5.16 MiB/s, done.
Total 8 (delta 0), reused 0 (delta 0)
remote: Compressing source files... done.
remote: Building source:
remote: 
remote: -----> Node.js app detected
remote:        
remote: -----> Creating runtime environment
remote:        
remote:        NPM_CONFIG_LOGLEVEL=error
remote:        NODE_ENV=production
remote:        NODE_MODULES_CACHE=true
remote:        NODE_VERBOSE=false
remote:        
remote: -----> Installing binaries
remote:        engines.node (package.json):  unspecified
remote:        engines.npm (package.json):   unspecified (use default)
remote:        
remote:        Resolving node version 12.x...
remote:        Downloading and installing node 12.15.0...
remote:        Using default npm version: 6.13.4
remote:        
remote: -----> Installing dependencies
remote:        Installing node modules (package.json + package-lock)
remote:        added 50 packages from 37 contributors and audited 126 packages in 1.68s
remote:        found 0 vulnerabilities
remote:        
remote:        
remote: -----> Build
remote:        
remote: -----> Caching build
remote:        - node_modules
remote:        
remote: -----> Pruning devDependencies
remote:        audited 126 packages in 0.98s
remote:        found 0 vulnerabilities
remote:        
remote:        
remote: -----> Build succeeded!
remote: -----> Discovering process types
remote:        Procfile declares types     -> (none)
remote:        Default types for buildpack -> web
remote: 
remote: -----> Compressing...
remote:        Done: 22.4M
remote: -----> Launching...
remote:        Released v3
remote:        https://damp-temple-40624.herokuapp.com/ deployed to Heroku
remote: 
remote: Verifying deploy... done.
To https://git.heroku.com/damp-temple-40624.git
 * [new branch]      master -> master

Testing of cloud services

URLs can be opened by the command:

heroku open -a damp-temple-40624

Open your browser and the Node.js service in just a few minutes of time on the cloud:

https://damp-temple-40624.herokuapp.com/user

summary

Heroku is a very good paas service, in addition to Node.js also supports other technology stack, we have time can try.

Item code

Reference Reading

https://devcenter.heroku.com/articles/getting-started-with-nodejs

Published 268 original articles · won praise 62 · Views 1.03 million +

Guess you like

Origin blog.csdn.net/starshus/article/details/104221737
Recommended