Code Monkey home page Code Monkey logo

docker-express-typescript-boilerplate's Introduction

Known Vulnerabilities Publish latest image

Express TypeScript Boilerplate with ChatGPT reviews

This repo can be used as a starting point for backend development with Nodejs. It comes bundled with Docker and is CI/CD optimized. The development environment uses docker-compose to start dependent services like mongo. There is an action to review all PRs with ChatGPT.

A few things to note in the project:

  • Github Actions Workflows - Pre-configured Github Actions to run automated builds and publish image to Github Packages.
  • Dockerfile - Dockerfile to generate docker builds.
  • docker-compose - Docker compose script to start service in production mode.
  • Containerized Mongo for development - Starts a local mongo container with data persistence across runs.
  • Safe Mongooose Connection Helper - A helper class to connect with Mongoose reliably.
  • Joi - For declarative payload validation
  • Middleware for easier async/await - Catches errors from routes and throws them to express error handler to prevent app crash due to uncaught errors.
  • OpenAPI 3.0 Spec - A starter template to get started with API documentation using OpenAPI 3.0. This API spec is also available when running the development server at http://localhost:3000/dev/api-docs
  • .env file for configuration - Change server config like app port, mongo url etc
  • Winston Logger - Uses winston as the logger for the application.
  • ESLINT - ESLINT is configured for linting.
  • Jest - Using Jest for running test cases

I. Installation

Using curl

$ bash <(curl -s https://raw.githubusercontent.com/sidhantpanda/public/master/scripts/generate-express-ts-app.sh)

Manual Method

1. Clone this repo

$ git clone [email protected]:sidhantpanda/docker-express-typescript-boilerplate.git your-app-name
$ cd your-app-name

2. Install dependencies

$ yarn install

II. Configuration

Update Docker repository for actions

$ yarn setup-actions

III. Development

Start dev server

Starting the dev server also starts MongoDB as a service in a docker container using the compose script at docker-compose.dev.yml.

$ yarn dev

Running the above commands results in

  • ๐ŸŒAPI Server running at http://localhost:3000
  • โš™๏ธSwagger UI at http://localhost:3000/dev/api-docs
  • ๐Ÿ›ข๏ธMongoDB running at mongodb://localhost:27017

IV. Packaging and Deployment

The mongo container is only only available in dev environment. When you build and deploy the docker image, be sure to provide the correct environment variables.

1. Build and run without Docker

$ yarn build && yarn start

2. Run with docker

$ docker build -t api-server .
$ docker run -t -i \
      --env NODE_ENV=production \
      --env MONGO_URL=mongodb://host.docker.internal:27017/books \
      -p 3000:3000 \
      api-server

3. Run with docker-compose

$ docker-compose up

Environment

To edit environment variables, create a file with name .env and copy the contents from .env.default to start with.

Var Name Type Default Description
NODE_ENV string development API runtime environment. eg: staging
PORT number 3000 Port to run the API server on
MONGO_URL string mongodb://localhost:27017/books URL for MongoDB

Logging

The application uses winston as the default logger. The configuration file is at src/logger.ts.

  • All logs are saved in ./logs directory and at /logs in the docker container.
  • The docker-compose file has a volume attached to container to expose host directory to the container for writing logs.
  • Console messages are prettified
  • Each line in error log file is a stringified JSON.

Directory Structure

+-- scripts
|   +-- dev.sh
|   +-- setup-github-actions.sh
+-- src
|   +-- controllers
|   |   +-- book
|   |   |   +-- add.ts
|   |   |   +-- all.ts
|   |   |   +-- get.ts
|   |   |   +-- index.ts
|   |   |   +-- remove.ts
|   |   |   +-- search.ts
|   +-- errors
|   |   +-- application-error.ts
|   |   +-- bad-request.ts
|   +-- lib
|   |   +-- safe-mongo-connection.ts
|   |   +-- winston-console-transport.ts
|   +-- middleware
|   |   +-- request-middleware.ts
|   +-- models
|   |   +-- Book.ts
|   +-- public
|   |   +-- index.html
|   +-- app.ts
|   +-- logger.ts
|   +-- routes.ts
|   +-- server.ts
+-- .env.default
+-- .eslintrc.json
+-- .gitignore
+-- .gitpod.yml
+-- docker-compose.dev.yml
+-- docker-compose.yml
+-- Dockerfile
+-- jest.config.js
+-- LICENSE
+-- nodemon.json
+-- openapi.yml
+-- package.json
+-- README.md
+-- renovate.json
+-- tsconfig.json
+-- yarn.lock

docker-express-typescript-boilerplate's People

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  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  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

docker-express-typescript-boilerplate's Issues

Project assumes nodemon and ts-node are installed globally

Hi, really like the project but I had a couple of issues.

dev.sh throws errors:

./scripts/dev.sh: line 25: nodemon: command not found
'ts-node' is not recognized as an internal or external command

Maybe include a notice in the README or add a script to package.json and add ts-node to devDependencies?

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Rate-Limited

These updates are currently rate-limited. Click on a checkbox below to force their creation now.

  • Update dependency nodemon to v3.1.3
  • Update dependency swagger-ui-express to v5.0.1
  • Update dependency ts-jest to v29.1.4
  • Update Node.js to v20.14.0 (node, @types/node)
  • Update dependency joi to v17.13.1
  • Update dependency eslint to v9
  • Update dependency supertest to v7
  • ๐Ÿ” Create all rate-limited PRs at once ๐Ÿ”

Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.

Ignored or Blocked

These are blocked by an existing closed PR and will not be recreated unless you click a checkbox below.

Detected dependencies

docker-compose
docker-compose.dev.yml
  • mongo 7.0
docker-compose.yml
dockerfile
.devcontainer/Dockerfile
  • mcr.microsoft.com/vscode/devcontainers/javascript-node 0-16
Dockerfile
  • node 20.11.1-alpine
  • node 20.11.1-alpine
github-actions
.github/workflows/build.yml
  • actions/checkout v3
  • actions/setup-node v3
  • actions/checkout v3
  • actions/setup-node v3
.github/workflows/code-review.yml
.github/workflows/latest.yml
  • actions/checkout v3
  • actions/setup-node v3
  • actions/checkout v3
  • actions/setup-node v3
  • actions/checkout v3
  • docker/login-action 65b78e6e13532edd9afa3aa52ac7964289d1a9c1
  • docker/metadata-action 3f6690a76cbcbe44da6bac853d66df365ab3af27
  • docker/build-push-action 817ed59f97d2974e2c1d92f170242f52fd3feae4
html
src/public/index.html
  • popper.js 1.14.7@sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1
npm
package.json
  • body-parser 1.20.2
  • compression 1.7.4
  • dotenv 16.4.5
  • express 4.18.3
  • joi 17.12.2
  • js-yaml ^4.1.0
  • mongoose 8.2.2
  • swagger-ui-express 5.0.0
  • winston 3.12.0
  • winston-transport 4.7.0
  • @types/compression 1.7.5
  • @types/express 4.17.21
  • @types/hapi__joi 17.1.14
  • @types/jest 29.5.12
  • @types/js-yaml ^4.0.9
  • @types/node 20.11.30
  • @types/supertest 6.0.2
  • @types/swagger-ui-express 4.1.6
  • @typescript-eslint/eslint-plugin 7.3.1
  • @typescript-eslint/parser 7.3.1
  • eslint 8.57.0
  • eslint-config-airbnb-base 15.0.0
  • eslint-plugin-import 2.29.1
  • jest 29.7.0
  • jest-express 1.12.0
  • nodemon 3.1.0
  • supertest 6.3.4
  • ts-jest 29.1.2
  • ts-node 10.9.2
  • typescript 5.4.2

  • Check this box to trigger a request for Renovate to run again on this repository

Fails to start with latest mongoose 5.11.18

Upgrading mongoose from 5.11.8 to 5.11.18 causes the server to crash on startup:

TSError: โจฏ Unable to compile TypeScript:
src/models/Book.ts:25:59 - error TS2345: Argument of type 'Schema<Document<any>, Model<Document<any>>, undefined>' is not assignable to parameter of type 'Schema<IBook, IBookModel, undefined>'.
  Types of property 'static' are incompatible.
    Type '{ (name: string, fn: (this: Model<Document<any>>, ...args: any[]) => any): Schema<Document<any>, Model<Document<any>>, undefined>; (obj: { ...; }): Schema<...>; }' is not assignable to type '{ (name: string, fn: (this: IBookModel, ...args: any[]) => any): Schema<IBook, IBookModel, undefined>; (obj: { [name: string]: (this: IBookModel, ...args: any[]) => any; }): Schema<...>; }'.
      Types of parameters 'fn' and 'fn' are incompatible.
        The 'this' types of each signature are incompatible.
          Type 'Model<Document<any>>' is not assignable to type 'IBookModel'.

25 const Book: IBookModel = model<IBook, IBookModel>('Book', schema);

License

Love the boilerplate! Could you put an MIT license on this?

ctrl + s doesn't restart the services

After Ctrl + S it saves the document but it needs to do npm run build && npm run start to run the updated code.. its takes lot of time to run the command again and again

Cannot load Swagger UI

Hi, great project! I like your choice of libs for nodejs web server development. ๐Ÿš€

There's a small problem though:
when I run npm run dev the server starts correctly but when I navigate to http://localhost:3000/dev/api-docs I get an HTTP 404 response
image

Any ideas why this might happen?

Middleware test fails

Hello, and first of all congratulation on this project!

I was trying to launch some commands and I noticed that when running tests I got a failure.

The code is on
__tests/middleware/request-middleware/test.ts

 FAIL  __tests/middleware/request-middleware.test.ts
  โ— Error Handling Middleware โ€บ Handover request to express error handler on error

    test error

      48 | 
      49 |      test('Handover request to express error handler on error', async () => {
    > 50 |              const err = new Error('test error')
         |                          ^
      51 |              const sampleRoute = async (req?: any, res?: any, nextHandler?: any) => {
      52 |                      throw err
      53 |              }

      at __tests/middleware/request-middleware.test.ts:50:15
      at __tests/middleware/request-middleware.test.ts:8:71
      at Object.<anonymous>.__awaiter (__tests/middleware/request-middleware.test.ts:4:12)
      at Object.<anonymous> (__tests/middleware/request-middleware.test.ts:49:72)```

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.