Git Product home page Git Product logo

nodejs-express-app's Introduction

WARNING: This repository is no longer maintained โš ๏ธ

This repository will not be updated. The repository will be kept available in read-only mode.

IBM Cloud

IBM Cloud platform Apache 2

Node.js Express starter application

This starter application is a simple microservice that incorporates the Express web framework to provide a landing page and two endpoints. Use this repository as a template for your own application.

A microservice is an individual component of an application that follows the microservice architecture - an architectural style that structures an application as a collection of loosely coupled services, each of which implements business capability. The microservice exposes a RESTful API matching a Swagger definition.

You can access the cloud native microservice capabilities at the following endpoints:

  • The Swagger UI is running on: /swagger/api-docs
  • Health endpoint: /health

The microservice is ready to run locally in a Docker container or with the Node.js runtime that is hosted on your local operating system. Use the IBM Cloud CLI Developer Tools commands (ibmcloud dev) for local development.

Deploying to IBM Cloud

IBM Cloud DevOps services provides toolchains as a set of tool integrations that support development, deployment, and operations tasks inside IBM Cloud.

Note: This application does not connect to external services. If you want to connect the app to external services, try the IBM Cloud Node.js + Cloudant starter application.

License

Copyright 2020 IBM

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.

You may obtain a copy of the license at http://www.apache.org/licenses/LICENSE-2.0.

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permission and limitations under the License.

What's included

  • Node.js application that functions as a microservice (server.js)
  • Rudimentary landing page and two endpoints (/health and /swagger/api-docs)
  • Extensible Node server code structure with directories for config, routes, and controllers
  • Simple functional and unit tests
  • Experience test script to verify your UI when the app is running
  • Dockerfiles for container deployment

If you use the IBM Cloud developer workflow, you also get:

  • Continuous delivery toolchain on IBM Cloud that includes a build and deploy pipeline
  • IBM Container Registry to hold your container images
  • GitLab source code repo with webhook to your pipeline
  • Deployment assets automatically injected into your repo as required by your target environment

Before you begin

Installing prereqs

Choosing a deployment target

Before you begin, you should decide on the type of deployment environment you want to target for your app. The IBM Cloud platform offers many types of environments, but the most common for cloud native apps are the IBM Kubernetes Service (IKS) or Red Hat OpenShift clusters.

We recommend that you create an IKS cluster ahead of time and deploy your application there. Creating an IKS cluster can take 15-30 minutes, but it's worth the wait. IKS runs your app inside orchestrated containers, and this option represents best-practice architecture for cloud native applications. To create an IKS cluster, you need a billable account. (That is, you either you need a credit card that is associated with your account for identity verification, or you need an IBM Cloud subscription). However, IKS offers a free cluster for 30 days, so you have plenty of time to try your app first.

Red Hat OpenShift, like IKS, is based on Kubernetes and represents best-practice architecture. OpenShift is especially good for hybrid environments as it can be run on IBM Cloud, in your own data center, or on the edge. OpenShift also requires a billable account but currently does not offer a free tier.

Configuring your starter application

This application comes ready to run and requires no explicit configuration.

Setting up and initializing your cloud development environment

Set up and initialize your development environment either from the IBM Cloud console or by using the IBM Cloud CLI. Both tools facilitate cloud native development and continuous delivery by creating a repo for you on IBM-hosted GitLab, copying the starter application code there, and then linking the repo to an automation toolchain that is powered by the IBM Cloud Continuous Delivery Service.

We recommend that you always set up and initialize a cloud development environment and deploy the starter application at least once before you make any changes to the application. By doing that, you can have a "known good" deployment to work from, and you don't need to migrate your code changes into the new environment. The remainder of these instructions assume that you do this.

Setting up from the IBM Cloud CLI

ibmcloud dev create

Follow the prompts and select the starter kit of interest (Node.js Express App) when the time comes. This sets up the CI/CD toolchain, creates a remote source code repo, clones the repo to your local machine, and starts the initial deployment.

Your machine must be configured for SSH access to your IBM Cloud GitLab account (for example, https://us-south.git.cloud.ibm.com/profile/keys) to clone the application code. For more information, see the GitLab docs.

Verifying that your cloud app is working

Internally, this starter app communicates on port 3000, but the exposed URL for your running application is determined by the runtime platform (Kubernetes or OpenShift). You can find a link to your running application in these ways:

  • Look for the link in the App details view for your app in the IBM Cloud console.
  • Look in the deployment logs that are produced by the deployment stage of your automated pipeline. See Debugging your cloud app for details about how to find these logs.
  • Look at the terminal output after you successfully run the ibmcloud dev create command.

Follow the app link or paste the URL into a browser to see the landing page for your new microservice. Check the endpoints that are provided by the microservice.

Debugging your cloud app

Pipeline logs

If your app is not working, check your pipeline status. Problems with build and deploy are displayed there, and from there you can view build and deploy logs. The pipeline is found in the DevOps toolchain for your app. You can find the toolchain in these ways:

  • Look for the link in the App details view for your app in the IBM Cloud console.
  • From the IBM Cloud main dashboard, select DevOps from the navigation menu, then select the region in which your toolchain resides. Find your toolchain in the list, and click it reveal the toolchain components. Click the pipeline component. You see links to logs in the tile for each stage in the pipeline.

Container logs

Container logs hold the information that's coming from your running app, similar to what you would see in the terminal when you run your app locally. Use the kubectl CLI to fetch logs for the problematic pod. For more information, see the docs for the kubectl logs command.

Developing locally

Compile and run your application in a local Docker container by using either the IBM Cloud CLI or the Docker CLI.

Be sure that you cd into the app root directory before you run these commands.

Using the IBM Cloud CLI

ibmcloud dev build
ibmcloud dev run

Press ctl-c on your keyboard to stop the container and return control to the command line.

Using the Docker CLI

docker build .
docker run -p 3000:3000 <my_image_id>

You can find the image ID in the console output of the docker build command. The -p flag represents publish and is necessary to expose the local endpoint outside the container. The IBM Cloud CLI does this implicitly.

Using the native runtime

npm install
npm start

Verifying that your local app is running

Your application is running at http://localhost:3000. Check the endpoints that are provided by the microservice.

Testing your app

The starter app repo contains unit tests, functional tests, and an experience test script to check the user-facing elements (UI and endpoints) that are presented by your application. The starter app also includes a linting mechanism.

Running tests and code coverage

To run tests and code coverage, use the following command:

 npm run test

A coverage folder is created with code coverage results that can be reviewed for gaps. The code coverage thresholds are also defined in package.json under nyc and can be adjusted if needed. Also, you can use the script npm run fix to automatically fix linting problems.

Pre-commit hooks

By default, a Husky pre-commit hook is defined in package.json, which runs linting, testing, and code coverage before the commit is made. If either the linting, testing, or code coverage fails, the pre-commit hook prevents the changes from being committed. Review the results and fix the problems, or modify the thresholds to allow the commit to succeed.

Experience tests

You can verify the state of your locally running application by using the Selenium UI test script that is included in the scripts directory.

Debugging your local app

You can build and debug your app locally by using the following command:

ibmcloud dev build --debug
ibmcloud dev debug

Deploying changes to IBM Cloud

Simply push to the master branch of your remote repo to initiate a new build/deploy cycle. After a few minutes, your modified app is running in the cloud. You can check the progress of the deployment by visiting the pipeline details view.

Getting support

If you have problems or questions when using the starter kit, you can go directly to the IBM Cloud Support Center to review featured FAQs, view status, or post questions on Stack Overflow. You can also open a support case.

CRA Scanning

This repository includes a .cracveomit file that is used by Code Risk Analyzer (CRA) in IBM Cloud Continuous Delivery. This file helps address vulnerabilities that are found by CRA until a remediation is available, at which point the vulnerabilities will be addressed in the respective package versions. CRA keeps the code in this repository free of known vulnerabilities, and therefore helps make applications that are built on this code more secure. If you are not using CRA, you can safely ignore this file.

Next steps


copyright: years: 2020, 2022


nodejs-express-app's People

Contributors

christoc-ibm avatar chuckcox avatar dependabot[bot] avatar gee4vee avatar jmeis avatar jruano03 avatar keithkade avatar mdarden avatar nfstein avatar reedcozart avatar stevemar avatar youming-lin avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

nodejs-express-app's Issues

Agressive testing

Hi Folks,

Really like this starter kit. Used it a couple times now.

Couple points:

  1. Somewhere (i guess on npm install?) it installs a nyt mocha pre-commit hook without asking me. This is (to me) a step too far and I'd want that to clearly say it's doing it and give me a chance to deny

  2. It turns on by default voting coverage tests that fail by default and linting errors that fail by default.

These two together mean my first experince with this thing is git clone, npm install, , git commit -> boom, errors I didn't expect.

How do you feel about making this more opt-in?

Thanks in advance!

Python executable missing from nodejs-10 UBI image

While trying to build the CF app, the npm install fails due to the fact python is not installed in the image and thus it can't perform node-gyp tasks that require compiling from source. The previous CF nodejs app used the official nodejs image which had python installed in it. This is a huge problem and will break almost any app whose deps require being built from source through npm. You can reach me internally at IBM, Brett Logan to answer any questions.

<tag_redacted> --rm --pull .
Waiting for Docker image to build
Sending build context to Docker daemon    469kB
Step 1/12 : FROM registry.access.redhat.com/ubi8/nodejs-10
latest: Pulling from ubi8/nodejs-10
Digest: sha256:a4cf131cc6d74235e82f7ae877f9d3c2300adea8715388457b63233b4a6ce703
Status: Image is up to date for registry.access.redhat.com/ubi8/nodejs-10:latest
 ---> 16b3f1306f73
Step 2/12 : WORKDIR /app
 ---> Using cache
 ---> 1817b9cd280c
Step 3/12 : USER root
 ---> Using cache
 ---> 8ee1fd1906a0
Step 4/12 : COPY --chown=1001:1001 package.json /app/
 ---> Using cache
 ---> 18a37f4864e4
Step 5/12 : RUN cd /app
 ---> Using cache
 ---> 57b750beaee7
Step 6/12 : RUN npm install --production
 ---> Running in 16600ecad2cf
npm WARN deprecated [email protected]: Please note that v5.0.1+ of superagent removes User-Agent header by default, therefore you may need to add it yourself (e.g. GitHub blocks requests without a User-Agent header).  This notice will go away with v5.0.2+ once it is released.
npm WARN deprecated [email protected]: CircularJSON is in maintenance only, flatted is its successor.
npm WARN deprecated [email protected]: This version has been deprecated in accordance with the hapi support policy (hapi.im/support). Please upgrade to the latest version to get the best features, bug fixes, and security patches. If you are unable to upgrade at this time, paid support is available for older versions (hapi.im/commercial).
> @ampretia/[email protected] install /app/node_modules/@ampretia/x509
> node-gyp rebuild
gyp ERR! configure error
gyp ERR! stack Error: Can't find Python executable "python", you can set the PYTHON env variable.
gyp ERR! stack     at PythonFinder.failNoPython (/usr/lib/node_modules/npm/node_modules/node-gyp/lib/configure.js:484:19)
gyp ERR! stack     at PythonFinder.<anonymous> (/usr/lib/node_modules/npm/node_modules/node-gyp/lib/configure.js:406:16)
gyp ERR! stack     at F (/usr/lib/node_modules/npm/node_modules/which/which.js:68:16)
gyp ERR! stack     at E (/usr/lib/node_modules/npm/node_modules/which/which.js:80:29)
gyp ERR! stack     at /usr/lib/node_modules/npm/node_modules/which/which.js:89:16
gyp ERR! stack     at /usr/lib/node_modules/npm/node_modules/isexe/index.js:42:5
gyp ERR! stack     at /usr/lib/node_modules/npm/node_modules/isexe/mode.js:8:5
gyp ERR! stack     at FSReqWrap.oncomplete (fs.js:153:21)
gyp ERR! System Linux 4.15.0-74-generic
gyp ERR! command "/usr/bin/node" "/usr/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js" "rebuild"
gyp ERR! cwd /app/node_modules/@ampretia/x509
gyp ERR! node -v v10.16.3
gyp ERR! node-gyp -v v3.8.0
gyp ERR! not ok
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! @ampretia/[email protected] install: `node-gyp rebuild`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the @ampretia/[email protected] install script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR!     /opt/app-root/src/.npm/_logs/2020-01-14T22_53_14_897Z-debug.log
The command '/bin/sh -c npm install --production' returned a non-zero code: 1
FAILED
An error exit status 1 was encountered while building the Docker
image.
exit status 1``` 

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.