Skip to content

Commit

Permalink
initial from main academy express template
Browse files Browse the repository at this point in the history
  • Loading branch information
nbogie committed Jan 31, 2024
0 parents commit 4dbe44a
Show file tree
Hide file tree
Showing 23 changed files with 3,921 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
DATABASE_URL=your-database-url-should-be-written-here
SESSION_SECRET=change_this_to_any_long_random_string_with_no_spaces2348fklvjcxbkjw3q3slo
22 changes: 22 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
module.exports = {
env: {
browser: false,
node: true,
commonjs: true,
es2021: true,
jest: true,
},
extends: "eslint:recommended",
overrides: [
{
files: [".eslintrc.{js,cjs}"],
parserOptions: {
sourceType: "script",
},
},
],
parserOptions: {
ecmaVersion: "latest",
},
rules: {},
};
33 changes: 33 additions & 0 deletions .github/workflows/ci-for-node.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# This workflow will do a clean installation of node dependencies, cache/restore them, build the source code and run tests across different versions of node
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-nodejs

name: Node.js CI

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

jobs:
build:
runs-on: ubuntu-latest

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

steps:
- uses: actions/checkout@v4
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
cache: "yarn"
- name: install dependencies with yarn
run: yarn install --frozen-lockfile
- run: yarn test
run: yarn lint
- name: Check code formatting with prettier
run: yarn format:check
132 changes: 132 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
.DS_Store

# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
.pnpm-debug.log*

# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage
*.lcov

# nyc test coverage
.nyc_output

# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# Snowpack dependency directory (https://snowpack.dev/)
web_modules/

# TypeScript cache
*.tsbuildinfo

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional stylelint cache
.stylelintcache

# Microbundle cache
.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variable files
.env
.env.development.local
.env.test.local
.env.production.local
.env.local

# parcel-bundler cache (https://parceljs.org/)
.cache
.parcel-cache

# Next.js build output
.next
out

# Nuxt.js build / generate output
.nuxt
dist

# Gatsby files
.cache/
# Comment in the public line in if your project uses Gatsby and not Next.js
# https://nextjs.org/blog/next-9-1#public-directory-support
# public

# vuepress build output
.vuepress/dist

# vuepress v2.x temp and cache directory
.temp
.cache

# Docusaurus cache and generated files
.docusaurus

# Serverless directories
.serverless/

# FuseBox cache
.fusebox/

# DynamoDB Local files
.dynamodb/

# TernJS port file
.tern-port

# Stores VSCode versions used for testing VSCode extensions
.vscode-test

# yarn v2
.yarn/cache
.yarn/unplugged
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.*
1 change: 1 addition & 0 deletions .node_version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
20
13 changes: 13 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Ignore artifacts:
build
coverage
dist

yarn.lock

**/*.log

# (Example) Tell prettier to ignore all HTML files:
# **/*.html

# version control and node_modules are ignored by default
3 changes: 3 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"tabWidth": 4
}
87 changes: 87 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
# Starter code for the academy tv-shows express web app project

This is an intermediate starter template for express apps written in JavaScript (not TypeScript).

It does not use express router - you can add that if you need it.

## Change this README.md file!

If you have used this project as a template, remember to change this readme file to add your own documentation and remove anything you don't need.

## Features

- EJS template setup with express
- Database support:

- connection-pool setup for [node-postgres](https://node-postgres.com/)
- loads DATABASE_URL from env variable. (And tries to load `.env` files with `dotenv`)

- live-reload
- automated testing with jest
- example jest test
- formatting with prettier
- linting with eslint
- workflow config for CI on github (see [.github/workflows/ci-for-node.yaml](.github/workflows/ci-for-node.yaml) )
- jsconfig.json (to enable vscode error-reporting in type-checked js files)
- express-session (only with flaky in-memory support. not meant for production use, only development)
- .gitignore
- logging with [morgan](https://expressjs.com/en/resources/middleware/morgan.html)
- demo of classless css framework ([sakura.css](https://oxal.org/projects/sakura/) or mvp.css, etc)
- static file serving from `/public`
- favicon
- TODO: error-handling

### note about live reload

In development mode, when viewing an html page, if a file is changed and saved, the browser will make the request again. This won't work if the browser is viewing a json output - it needs to be an html page so that live-reload can insert a javascript fragment into it.

## Installation

Install dependencies

`yarn`

## Configuration on dev machine

Copy `.env.example` to `.env` and set any variables there appropriately:

- `DATABASE_URL` to your database's connection string.
- `SESSION_SECRET` to a long very random string with no spaces, unique to your application.

If your express app isn't going to need sessions, you can remove the session-setup code from the express setup (probably in `setupExpress.js`) and this will remove the need for the related environment variable.

## Running

Before you change any code (with exception of creating a suitable `.env` file), check that the app runs and handles a request to `/`.

(It's ok if your database doesn't exist yet, provided you don't make request to `/db-test`)

Run (in dev mode with live-reload):

`yarn start:dev`

Run (for production with no live-reload)

`yarn start`

## Running tests

`yarn test`

## Linting

`yarn lint`

## Formatting with prettier

`yarn format`

However, it is suggested you install vscode's prettier extension and enable the user setting `format on save`. When formatting, VSCode will notice the .prettierrc and format according to those rules (and prettier's defaults).

## CI (linting, formatting check, automated tests)

This project includes a workflow file in [.github](.github) which will cause CI to run on github.

## Debugging with vscode

Set a breakpoint in the margin of any JS file, and use run-and-debug (ctrl-shift-d)'s `Run and Debug` button to start express. Bear in mind that a breakpoint set in a request handler won't cause express to pause until a matching request comes in!
9 changes: 9 additions & 0 deletions jsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"compilerOptions": {
"module": "commonjs",
"target": "esnext",
"checkJs": true,
},
"exclude": ["node_modules", "**/node_modules/*"],
"include": ["src/**/*"],
}
33 changes: 33 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"name": "academy-express-tv-shows-project-starter",
"version": "1.0.0",
"main": "src/app.js",
"author": "nbogie",
"license": "MIT",
"scripts": {
"start": "node src/app.js",
"start:dev": "nodemon src/app.js --ext 'js ejs html json css'",
"test": "jest --verbose",
"lint": "yarn eslint .",
"format": "yarn prettier --write .",
"format:check": "yarn prettier ."
},
"dependencies": {
"cors": "^2.8.5",
"dotenv": "^16.4.1",
"ejs": "^3.1.9",
"express": "^4.18.2",
"express-session": "^1.17.3",
"morgan": "^1.10.0",
"pg": "^8.11.3"
},
"devDependencies": {
"@types/jest": "^29.5.11",
"connect-livereload": "^0.6.1",
"eslint": "^8.56.0",
"jest": "^29.7.0",
"livereload": "^0.9.3",
"nodemon": "^3.0.3",
"prettier": "^3.2.4"
}
}
Binary file added public/favicon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 14 additions & 0 deletions public/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
* {
box-sizing: border-box;
}

body {
margin: 0 auto;
max-width: 1200px;
/* height: 100vh; */
}

.product {
display: grid;
grid-template-columns: 3fr 0.4fr 0.4fr 0.4fr 1fr 2fr;
}
31 changes: 31 additions & 0 deletions src/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const { app } = require("./support/setupExpress");
const { query } = require("./support/db");

//configure the server's route handlers
app.get("/", (req, res) => {
res.render("pages/index");
});

app.get("/db-test", async (req, res) => {
try {
const dbResult = await query("select now()");
const rows = dbResult.rows;
res.json(rows);
} catch (err) {
console.error(err);
res.status(500).send(
"Sorry, an error occurred on the server. Ask the dev team to check the server logs at time " +
new Date(),
);
}
});

// use the environment variable PORT, or 3000 as a fallback if it is undefined
const PORT_NUMBER = process.env.PORT ?? 3000;

//start the server listening indefinitely
app.listen(PORT_NUMBER, () => {
console.log(
`Your express app started listening on ${PORT_NUMBER} at ${new Date()}`,
);
});
Loading

0 comments on commit 4dbe44a

Please sign in to comment.