Coder Social home page Coder Social logo

example-api's Introduction

Express with Typescript

Getting started

Initialize npm project by running the following command:

npm init -y

Install Express

npm install express

Install Typescript

npm install --save-dev typescript

Setup tsconfig.json file

npx tsc --init

Set the config file to looks similar to the following:

{
  "compilerOptions": {
    "target": "es2016",
    "module": "commonjs",
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true,
    "strict": true,
    "skipLibCheck": true,
    "outDir": "./dist"
  }
}

target - Leave the default option es2016. module - Leave the default option commonjs. skipLibCheck - Setting this to true can help time. outDir sets where the compiled file will be generated.

Setup eslint

npx eslint --init

Install type support for express

npm i -D typescript @types/express

Build and run

npm run Build
npm start

Setup docker Create a new Dockerfile and add the content from below.

FROM node:18-alpine AS build-env
WORKDIR /app
# copy source files
COPY ./src/ ./src/
# copy required config files
COPY tsconfig.json package.json package-lock.json ./

# update npm to latest version
RUN npm install -g [email protected]

# install dependencies and build
RUN npm ci
RUN npm run build

# this step will create the actual docker image used for deployment
FROM gcr.io/distroless/nodejs18-debian11
COPY --from=build-env /app /app
WORKDIR /app
CMD ["dist/index.js"]

To create the image run

docker build -t example-api .

To run the image run

docker run --rm -p 8080:8080 -t example-api

Adding unit testing

We will use ava as the unit test runner. To start run the command:

npm init ava

Setup config inside package.json

  "ava": {
    "nodeArguments": [
      "--loader=ts-node/esm"
    ],
    "typescript": {
      "compile": "tsc",
      "rewritePaths": {
        "src/": "dist/"
      }
    }
  }

Add coverage support

npm install --save-dev c8

Update test (in package.json) script to use coverage

{
   "test": "c8 ava"
}

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.