Install Yarn if needs be with npm i -g yarn
.
-
mkdir my-app && cd my-app && npm init -y
-
yarn add express cors morgan dotenv
-
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
-
npx tsc --init
-
(if not in Git repo already):
git init
-
npm init @eslint/config
-
yarn add --dev prettier eslint-config-prettier
-
in ESLint config:
- add
"prettier"
toextends
, e.g.extends: [...other confs..., 'prettier']
- under
parserOptions
, addproject: './tsconfig.json'
- add
-
Add
.prettierrc.json
(see other file in this gist) -
echo node_modules >> .gitignore && git add .
-
mkdir src && touch src/index.ts
-
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}`));