Skip to content

Commit

Permalink
Merge pull request #35 from timothywarner/tim-feature
Browse files Browse the repository at this point in the history
Tim feature
  • Loading branch information
mikepfeiffer authored Aug 26, 2024
2 parents 869592e + 7582584 commit 775283e
Show file tree
Hide file tree
Showing 20 changed files with 3,340 additions and 571 deletions.
6 changes: 6 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
version: 2
updates:
- package-ecosystem: "npm"
directory: "/"
schedule:
interval: "daily"
52 changes: 52 additions & 0 deletions .github/workflows/azure.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# This workflow will build and push a node.js application to an Azure Web App when a release is created.
#
# This workflow assumes you have already created the target Azure App Service web app.
# For instructions see https://docs.microsoft.com/azure/app-service/app-service-plan-manage#create-an-app-service-plan
#
# To configure this workflow:
#
# 1. For Linux apps, add an app setting called WEBSITE_WEBDEPLOY_USE_SCM and set it to true in your app **before downloading the file**.
# For more instructions see: https://docs.microsoft.com/azure/app-service/configure-common#configure-app-settings
#
# 2. Set up a secret in your repository named AZURE_WEBAPP_PUBLISH_PROFILE with the value of your Azure publish profile.
# For instructions on obtaining the publish profile see: https://docs.microsoft.com/azure/app-service/deploy-github-actions#configure-the-github-secret
#
# 3. Change the values for the AZURE_WEBAPP_NAME, AZURE_WEBAPP_PACKAGE_PATH and NODE_VERSION environment variables (below).
#
# For more information on GitHub Actions for Azure, refer to https://github.com/Azure/Actions
# For more samples to get started with GitHub Action workflows to deploy to Azure, refer to https://github.com/Azure/actions-workflow-samples
name: azure-deploy

on:
# release:
# types: [created]

env:
AZURE_WEBAPP_NAME: twaz104webapp1 # set this to your application's name
AZURE_WEBAPP_PACKAGE_PATH: '.' # set this to the path to your web app project, defaults to the repository root
NODE_VERSION: '10.x' # set this to the node version to use

jobs:
build-and-deploy:
name: Build and Deploy
runs-on: ubuntu-latest
environment: production
steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ env.NODE_VERSION }}
uses: actions/setup-node@v1
with:
node-version: ${{ env.NODE_VERSION }}
- name: npm install, build, and test
run: |
# Build and test the project, then
# deploy to Azure Web App.
npm install
npm run build --if-present
npm run test --if-present
- name: 'Deploy to Azure WebApp'
uses: azure/webapps-deploy@v2
with:
app-name: ${{ env.AZURE_WEBAPP_NAME }}
publish-profile: ${{ secrets.AZURE_WEBAPP_PUBLISH_PROFILE }}
package: ${{ env.AZURE_WEBAPP_PACKAGE_PATH }}
30 changes: 30 additions & 0 deletions .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# This workflow will do a clean install of node dependencies, build the source code and run tests across different versions of node
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions

name: Node.js CI

on:
push:
branches: [ master ]
pull_request:
branches: [ master ]

jobs:
build:

runs-on: ubuntu-latest

strategy:
matrix:
node-version: [10.x, 12.x, 14.x, 15.x]
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/

steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}
- run: npm ci
- run: npm run build --if-present
- run: npm test
4 changes: 4 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"appService.deploySubpath": ".",
"appService.defaultWebAppToDeploy": "/subscriptions/2fbf906e-1101-4bc0-b64f-adc44e462fff/resourceGroups/appsvc_linux_centralus_basic/providers/Microsoft.Web/sites/twnodetestapp"
}
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Node & Express Demo App for Azure DevOps

## Last edited by Tim Warner

> Build Your First CI/CD Pipeline using Azure DevOps with this Demo App.
This is a Node and Express web application used to demonstrate CI/CD with Azure DevOps. You can clone this repo and use it within Azure DevOps to build, test, and release to an Azure App Service web app.
Expand Down
21 changes: 15 additions & 6 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,33 @@
const config = require('./config');
const express = require('express');
const path = require('path');
const exphbs = require('express-handlebars');
const { engine } = require('express-handlebars');

const index = require('./routes/index');
const who = require('./routes/who');
const contact = require('./routes/contact');

const app = express();

// Set the directory where the view templates are located
app.set('views', path.join(__dirname, 'views'));
app.engine('handlebars', exphbs({defaultLayout: 'main'}));

// Initialize and set the view engine to handlebars using express-handlebars
app.engine('handlebars', engine({ defaultLayout: 'main' }));
app.set('view engine', 'handlebars');

// Set the port from the configuration
app.set('port', config.port);

app.use('/', express.static('public'))
// Middleware to serve static files from the 'public' directory
app.use('/', express.static('public'));

// Route middleware
app.use('/', index);
app.use('/who', who);
app.use('/contact', contact);

app.listen(config.port, () => {
console.log(`Demo app is running on ${config.port}!`);
});
// Start the server
app.listen(app.get('port'), () => {
console.log(`Server running on port ${app.get('port')}`);
});
63 changes: 63 additions & 0 deletions azure-pipelines-2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
trigger:
- master

variables:
azureSubscription: '<Azure-Subscription-Name>'
webAppName: '<App-Service-Name>'
environmentName: 'production'
nodeVersion: '14.x'
workingDirectory: '.' # update this to the path to your web app project, if applicable

stages:
- stage: Build
displayName: Build stage
jobs:
- job: Build
displayName: Build
pool:
vmImage: 'ubuntu-latest'
steps:
- task: NodeTool@0
inputs:
versionSpec: $(nodeVersion)
displayName: 'Install Node.js'

- script: |
npm install
npm run build --if-present
displayName: 'npm install and build'
workingDirectory: $(workingDirectory)
- task: ArchiveFiles@2
displayName: 'Archive files'
inputs:
rootFolderOrFile: '$(System.DefaultWorkingDirectory)'
includeRootFolder: false
archiveType: 'zip'
archiveFile: $(Build.ArtifactStagingDirectory)/$(Build.BuildId).zip
replaceExistingArchive: true

- upload: $(Build.ArtifactStagingDirectory)/$(Build.BuildId).zip
artifact: drop

- stage: Deploy
displayName: Deploy stage
dependsOn: Build
condition: succeeded()
jobs:
- deployment: Deploy
displayName: Deploy
environment: $(environmentName)
pool:
vmImage: 'ubuntu-latest'
strategy:
runOnce:
deploy:
steps:
- task: AzureRmWebAppDeployment@4
inputs:
ConnectionType: 'AzureRM'
azureSubscription: $(azureSubscription)
appType: 'webApp'
WebAppName: $(webAppName)
packageForLinux: '$(Pipeline.Workspace)/drop/$(Build.BuildId).zip'
2 changes: 1 addition & 1 deletion azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ steps:

- task: PublishBuildArtifacts@1
displayName: 'Publish artifacts: drop'

2 changes: 1 addition & 1 deletion azuredeploy.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"site_host_name": {
Expand Down
2 changes: 1 addition & 1 deletion config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module.exports = {
port: process.env.PORT || 3000,
port: process.env.PORT || 443,
};
79 changes: 79 additions & 0 deletions debug-solution.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
To introduce common Node.js debugging scenarios into your application for teaching purposes, here are several examples that simulate typical issues developers might encounter. These scenarios are designed to be instructional without breaking the application entirely:

### 1. Dependency Version Conflict

**Scenario**: Intentionally downgrade a package version in [`package.json`](command:_github.copilot.openRelativePath?%5B%7B%22scheme%22%3A%22file%22%2C%22authority%22%3A%22%22%2C%22path%22%3A%22%2FC%3A%2Fgithub%2Farchive%2Fnode-express-azure%2Fpackage.json%22%2C%22query%22%3A%22%22%2C%22fragment%22%3A%22%22%7D%5D "c:\github\archive\node-express-azure\package.json") to simulate a dependency version conflict. For example, change the version of `express` to an older one that might lack some features used in the app.

```json
"dependencies": {
"express": "4.16.0" // Downgrade from a newer version
}
```

**Teaching Point**: Demonstrate how to identify and resolve dependency version conflicts using `npm list`, `npm outdated`, and reading the package's changelog to understand breaking changes.

### 2. Async/Await Misuse

**Scenario**: Introduce a bug by removing `await` from an asynchronous function call, causing unexpected behavior due to the promise not being resolved.

```javascript
// Original
const result = await someAsyncFunction();

// Modified (introduce bug)
const result = someAsyncFunction();
```

**Teaching Point**: Highlight the importance of correctly using `async/await` for handling asynchronous operations and how to debug issues related to promises and asynchronous code.

### 3. Environment Configuration Error

**Scenario**: Misconfigure an environment variable in a way that doesn't crash the app but causes it to behave unexpectedly. For example, set the port number to a string that can't be converted to a number.

```javascript
// In config.js or .env
PORT="notAPort"
```

**Teaching Point**: Teach how to debug configuration issues, including checking environment variables and ensuring they are correctly parsed and validated.

### 4. Incorrect Middleware Order

**Scenario**: Rearrange middleware in `app.js` so that a middleware that should run after another runs before it, such as placing error handling middleware too early in the stack.

```javascript
// Before
app.use('/api', apiRoutes);
app.use(errorHandler);

// After (introduce bug)
app.use(errorHandler);
app.use('/api', apiRoutes);
```

**Teaching Point**: Explain the importance of middleware order in Express applications and how to trace and resolve issues arising from incorrect middleware sequencing.

### 5. Callback Error Handling

**Scenario**: Introduce a subtle bug in a callback function by not properly handling an error scenario, which might cause unhandled exceptions or the application to hang.

```javascript
// Original
fs.readFile('somefile.txt', (err, data) => {
if (err) throw err;
console.log(data);
});

// Modified (introduce bug)
fs.readFile('somefile.txt', (err, data) => {
console.log(data);
});
```

**Teaching Point**: Discuss the importance of error handling in callbacks and how to properly handle errors to prevent crashes and ensure application robustness.

These scenarios cover a range of common issues from simple syntax and logical errors to more complex asynchronous programming and configuration challenges, providing a comprehensive debugging learning experience.




8 changes: 8 additions & 0 deletions node-express-azure.code-workspace
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"folders": [
{
"path": "."
}
],
"settings": {}
}
Loading

0 comments on commit 775283e

Please sign in to comment.