Skip to content

Instantly share code, notes, and snippets.

@bhubr
Last active March 1, 2023 06:11
Show Gist options
  • Save bhubr/28fe3ad88a569018bd32702f61f0e22d to your computer and use it in GitHub Desktop.
Save bhubr/28fe3ad88a569018bd32702f61f0e22d to your computer and use it in GitHub Desktop.
Quick Express + TypeScript setup
{
"singleQuote": true
}

Install Yarn if needs be with npm i -g yarn.

  1. mkdir my-app && cd my-app && npm init -y

  2. yarn add express cors morgan dotenv

  3. yarn add --dev typescript ts-node nodemon @types/express @types/cors @types/morgan @types/node eslint @typescript-eslint/eslint-plugin@latest @typescript-eslint/parser@latest

  4. npx tsc --init

  5. (if not in Git repo already): git init

  6. npm init @eslint/config

  7. yarn add --dev prettier eslint-config-prettier

  8. in ESLint config:

    • add "prettier" to extends, e.g. extends: [...other confs..., 'prettier']
    • under parserOptions, add project: './tsconfig.json'
  9. Add .prettierrc.json (see other file in this gist)

  10. echo node_modules >> .gitignore && git add .

  11. mkdir src && touch src/index.ts

  12. in package.json's scripts: "dev": "nodemon --exec ts-node src/index.ts"

Contents of src/index.ts:

import express, { Request, Response } from "express";
import cors from "cors";
import morgan from "morgan";
import dotenv from "dotenv";

dotenv.config();

const app = express();
app.use(express.json());
app.use(cors());
app.use(morgan("dev"));

app.get("/", (req: Request, res: Response) =>
  res.send({
    message: "Hello world!",
  })
);

const port = process.env.PORT || 8000;

app.listen(port, () => console.log(`listening on ${port}`));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment