Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
79 commits
Select commit Hold shift + click to select a range
eecc8ce
ignore lock files
pakeku May 11, 2025
6664708
update documenation
pakeku May 11, 2025
29a4f86
chore: remove unused/old documentation
pakeku May 11, 2025
fbcc164
relete repeated code, move app code into app file
pakeku May 11, 2025
29683c5
refactor: improve server startup logic with async/await and error han…
pakeku May 11, 2025
39b2d48
clean up
pakeku May 11, 2025
ae90b63
chore: update mongo to latest
pakeku May 11, 2025
11216f9
chore: reorganize files into their own files
pakeku May 11, 2025
cbe25d4
add supertest tests
pakeku May 11, 2025
799a490
fix: ObjectId updated to current
pakeku May 11, 2025
da6d7b7
Merge pull request #1 from pakeku/erick/update-documentation
pakeku May 11, 2025
9234317
enhance: better structure, remove git-user-name to remove vulnerability
pakeku May 11, 2025
d0f3ed2
Hooked into SIGINT and SIGTERM signals to gracefully close the HTTP s…
pakeku May 11, 2025
a431cae
all tests passing
pakeku May 11, 2025
08ec030
feat: set up for testing
pakeku May 11, 2025
c573c3a
enhance documentation
pakeku May 11, 2025
cdf05fc
enhance documentation, refactor code
pakeku May 11, 2025
b21bd4f
Create tests.yml
pakeku May 11, 2025
efb59ee
Update tests.yml
pakeku May 11, 2025
d965389
Update tests.yml
pakeku May 11, 2025
3bab518
Update tests.yml
pakeku May 11, 2025
95221bf
Merge pull request #5 from pakeku/erick/update-documentation
pakeku May 11, 2025
7b13a12
update documentation
pakeku May 11, 2025
2174ccf
add test badge
pakeku May 11, 2025
65dadb8
Merge pull request #6 from pakeku/erick/update-documentation
pakeku May 11, 2025
eb8ede4
update badge
pakeku May 11, 2025
4ef9f86
Merge pull request #7 from pakeku/erick/update-documentation
pakeku May 11, 2025
c35aea0
include CORS variables
pakeku May 11, 2025
c1ef70f
uninstall archiver due to no use and vulnerability
pakeku May 11, 2025
6078ad2
Create snyk.yml
pakeku May 11, 2025
1b7e557
remove unused npm package
pakeku May 11, 2025
aff2c58
Update snyk.yml
pakeku May 11, 2025
9807ef1
Merge pull request #8 from pakeku/erick/security-vulnerability-check
pakeku May 11, 2025
1cbb80e
remove duplicate setup
pakeku May 11, 2025
9baccbe
Merge pull request #9 from pakeku/erick/document-enhance
pakeku May 11, 2025
ffc14a0
chore(security): disable x-powered-by header to reduce information ex…
pakeku May 11, 2025
e57fa14
send back json to prevent xss
pakeku May 11, 2025
ec44e67
Merge pull request #10 from pakeku/erick/document-enhance
pakeku May 11, 2025
9d15a1c
add snyk badge;
pakeku May 11, 2025
25c63c8
Merge pull request #11 from pakeku/erick/document-enhance
pakeku May 11, 2025
b52c327
add authentication with jwt
pakeku May 11, 2025
7267907
Update src/routes/authRoute.js
pakeku May 11, 2025
84896cc
Update src/routes/authRoute.js
pakeku May 11, 2025
86667b1
Merge pull request #12 from pakeku/erick/document-enhance
pakeku May 11, 2025
58539f8
feat: migrate to ts
pakeku May 12, 2025
ebd5789
fix: update return field for update
pakeku May 17, 2025
2f89eba
update NODE_ENV to "test" for out of the box ready experience
pakeku May 17, 2025
97dce93
Merge pull request #13 from pakeku/erick/ts-migration
pakeku May 17, 2025
0964c3a
feat: get profile info
pakeku May 17, 2025
7f4e25e
fix: add documentation about JWT_SECRET
pakeku May 17, 2025
453822d
fix build output and start command, update documentation
pakeku May 17, 2025
b535c5c
use err message returned
pakeku May 17, 2025
533f3f3
remove unused variable
pakeku May 17, 2025
98dc269
enhancement: add prettier and eslint for consistent code
pakeku May 17, 2025
ba1f6aa
fix wrong response body check on test
pakeku May 17, 2025
0728c34
fix bug re: failed test
pakeku May 17, 2025
030edb9
Update tests.yml
pakeku May 17, 2025
f22ef8d
Update tests.yml
pakeku May 17, 2025
ca9dffa
fix store responses
pakeku May 17, 2025
11cf818
This commit enhances the function to detect the execution
pakeku May 17, 2025
0a84169
Merge pull request #16 from pakeku/erick/profile
pakeku May 17, 2025
c1d9928
add auth middleware, correct type in git-username
pakeku May 17, 2025
3be402f
make the intent of the condition clearer and reduce potential confusion
pakeku May 17, 2025
270a13b
Merge pull request #17 from pakeku/erick/profile
pakeku May 17, 2025
ba03b04
update documentation
pakeku May 17, 2025
2a122c5
Merge pull request #18 from pakeku/erick/profile
pakeku May 17, 2025
e26c441
feat: add swagger documentation
pakeku May 17, 2025
ab14e0c
update documentation, orgnize
pakeku May 17, 2025
31c8849
format
pakeku May 17, 2025
905023f
add type:module
pakeku May 17, 2025
e1e20b1
Merge pull request #19 from pakeku/documentation/swagger
pakeku May 17, 2025
48fedea
remove type:module
pakeku May 17, 2025
012a894
change config to use module.exports instead of export default
pakeku May 17, 2025
751b470
lint
pakeku May 17, 2025
fc3a162
increase code coverage
pakeku May 17, 2025
71f800c
Merge pull request #20 from pakeku/documentation/swagger
pakeku May 17, 2025
c38267e
remove console.error log
pakeku May 18, 2025
0ca10f4
improve docs
pakeku May 18, 2025
c1fa109
Merge pull request #21 from pakeku/authentication/jwt
pakeku May 18, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 0 additions & 39 deletions .circleci

This file was deleted.

1 change: 0 additions & 1 deletion .env-sample

This file was deleted.

36 changes: 36 additions & 0 deletions .env.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# 📦 .env.sample
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice job adding an env sample

# Copy this file to .env and fill in the actual values
# Command: cp .env.sample .env

# === MongoDB Configuration ===

# MongoDB connection URI for development/production environments.
# Format: mongodb://<username>:<password>@<host>:<port>/<database>
# Example: mongodb://user:pass@localhost:27017/mydatabase
MONGO_URL='mongodb://your_username:your_password@host:port/database_name'

# Set the environment
# NODE_ENV can be 'development', 'production', or 'test'
# When set to `"test"`, a test database is used, and no `MONGO_URL` is required. This allows for out-of-the-box testing without a live database.
NODE_ENV=test

# === Notes ===
# Do NOT use real credentials in this file.
# In production, make sure this file is excluded from version control.
# For test, the in-memory MongoDB server will be used automatically if NODE_ENV=test

# === CORS Configuration ===
# CORS (Cross-Origin Resource Sharing) settings
# By default, CORS is disabled.
# Default methods: GET, POST, PUT, DELETE
ALLOWED_ORIGINS=
ALLOWED_METHODS=
ALLOWED_HEADERS=

# === JWT Configuration ===
# JWT (JSON Web Token) secret key for signing tokens
# A cryptographically secure secret used to sign and verify JSON Web Tokens (JWTs).
# This is required for authentication to work correctly.
# 🔐 Use a long, random string—at least 32 characters, ideally generated using a password manager or Node.js:
# $ node -e "console.log(require('crypto').randomBytes(64).toString('hex'))"
JWT_SECRET=
37 changes: 37 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: Backend Unit Tests

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

jobs:
build:
runs-on: ubuntu-latest

strategy:
matrix:
node-version: [18.x, 20.x, 22.x]

steps:
- uses: actions/checkout@v4

- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}

# Install dependencies (without using lock file)
- name: Install dependencies
run: npm install

# Ensure consistent installs with npm ci
- name: Run npm ci (ensure clean node_modules)
run: npm ci

- name: Run tests
run: npm test
env:
NODE_ENV: test
JWT_SECRET: ${{ secrets.JWT_SECRET }}
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ build/Release
# Dependency directories
node_modules/
jspm_packages/
package-lock.json
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sometimes good to have package lock committed so the CI can use it with the exact packages it was committed with. Recomend removing it from the ignore.

yarn.lock

# Typescript v1 declaration files
typings/
Expand Down Expand Up @@ -67,3 +69,6 @@ typings/
.elasticbeanstalk/*
!.elasticbeanstalk/*.cfg.yml
!.elasticbeanstalk/*.global.yml

# Test Coverage
coverage
3 changes: 3 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
dist
node_modules
.github
7 changes: 7 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice

"printWidth": 100,
"semi": true,
"singleQuote": true,
"trailingComma": "es5",
"arrowParens": "avoid"
}
6 changes: 6 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"accessibility.signals.chatRequestSent": {
"sound": "off",
"announcement": "off"
}
}
25 changes: 0 additions & 25 deletions AWS.md

This file was deleted.

73 changes: 0 additions & 73 deletions HEROKU.md

This file was deleted.

135 changes: 54 additions & 81 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,81 +1,54 @@
# Node.js and Express Tutorial: Building and RESTful APIs

## Requirements

Identify your mongo db url used previously. Ask JR for a shared mongo db url if needed.

We will configure the mongo db url on aws as well as in our local environment.

## Objectives

Be able to do the following:

### AWS

- understand changes required for production deployment to aws

- update your week-10 storefront api for deployment to aws

- deploy your updated repository to aws

- add your mongo db url as an environment variable on aws

### Heroku

- understand heroku deployment

- install the heroku cli

- login to heroku from the cli

- configure environment variables on heroku

## Overview

This readme outlines all of the changes included in this repository that allow aws deployment.

After an overview, you will make changes to your week 10 repository and deploy it to aws.

You will commit your changes to your week 10 repository and create a new PR.

## AWS and Heroku Deployment

New this week is AWS and Heroku deployment and the changes that were made to support the deployment process.

Below we list out changes required from the original development-only version we previously built.

### package.json

- Changed the "start" script to use "node" in place of "nodemon". Nodemon is for development only.

- Added a "dev" script to support using nodemon in development.

- Added a "zip-for-aws" script to zip content for deployment to aws.

- Installed `env-cmd` and `archiver` node modules

`env-cmd` allows us to have a `.env` file in development to configure our MONGO db url safely where the setting is not shared in git.

`archiver` supports a script to run to generate the `zip` file that aws requires for the aws web console upload.

### index.js

- Updated "port" settings to allow the production server to set the port value.

- Added configuration check for MONGO_URL environment variable and start DB only when configured.

### AWS and Heroku

See AWS.md and HEROKU.md

## Previously in the repo

We built this repo in week 10 and then updated it for week 12 as an introduction to connecting an express node app to a mongo database.

This app was configured originally only for development and required additional work this week for production readiness.

Previous versions:

- Week 10: <https://github.com/SummerOfCode2020/week-10-store-manager-api>

- Week 12: <https://github.com/SummerOfCode2020/week-12-store-manager-api>
# Node.js and Express Backend
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow - nicely improved version.


[![Backend API - CI Tests](https://github.com/pakeku/backend-api/actions/workflows/tests.yml/badge.svg)](https://github.com/pakeku/backend-api/actions/workflows/tests.yml)
[![Known Vulnerabilities](https://snyk.io/test/github/pakeku/backend-api/badge.svg)](https://snyk.io/test/github/pakeku/backend-api)
[![code Style: Prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat&logo=prettier)](https://prettier.io/)
[![ESLint](https://img.shields.io/badge/linting-eslint-blue.svg?style=flat&logo=eslint)](https://eslint.org/)
[![TypeScript](https://img.shields.io/badge/language-typescript-blue.svg?style=flat&logo=typescript)](https://www.typescriptlang.org/)
[![Swagger UI](https://img.shields.io/badge/docs-Swagger_UI-blue?logo=swagger)](http://localhost:3000/api-docs)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could be good to specify local here since we have localhost hardcoded here.


## Configuration

You can define your environmental variables in a `.env` file at the root of the project. (Start by copying `.env.sample``.env`).\
**⚠️ Important:** Never commit your `.env` file to version control.

| Variable | Required | Description | Example |
| ----------------- | ---------- | ---------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------- |
| `MONGO_URL` | ✅ Yes | Connection string for MongoDB. Obtain this from your MongoDB provider. | `mongodb+srv://<username>:<password>@<cluster-url>/<database-name>?retryWrites=true&w=majority` |
| `PORT` | ❌ No | Port for the Express server to listen on. Defaults to `3000`. | `8080` |
| `ALLOWED_ORIGINS` | ❌ No | Comma-separated list of allowed origins for CORS. | `http://localhost:3000,https://your-frontend.com` |
| `ALLOWED_METHODS` | ❌ No | Comma-separated list of allowed HTTP methods for CORS. | `GET,POST,PUT,DELETE` |
| `ALLOWED_HEADERS` | ❌ No | Comma-separated list of allowed request headers for CORS. | `Content-Type,Authorization` |
| `NODE_ENV` | ⚠️ Depends | Application environment: `development`, `production`, or `test`. `MONGO_URL` not required in `test`. | `development` |
| `JWT_SECRET` | ✅ Yes | Secret key used for signing/verifying JWTs. Must be secure and private. | `Use: node -e "console.log(require('crypto').randomBytes(64).toString('hex'))"` |

## Getting Started

1. Install Dependencies

```bash
npm run install
```

2. Create .env file and gather your variable values.

```bash
cp .env.sample .env
```

3. Run script:

```json
"scripts": {
"prebuild":"rm -rf dist",
"build":"tsc",
"start": "node ./src/index.js",
"dev": "env-cmd nodemon ./src/index.ts",
"test": "jest",
"test:watch": "jest --watch",
"lint": "eslint . --ext .ts",
"lint:fix": "eslint . --ext .ts --fix",
"lint:check": "eslint . --ext .ts --no-ignore",
"format": "prettier --write ."
}
```
Loading