Express with Typescript

오픈소스·2023년 3월 6일
0
post-thumbnail

typescript-express-starter도 있지만, step by step으로 작성해 봅니다.

$ mkdir express_with_typescript
$ cd express_with_typescript
$ npm init --yes
$ npm install express dotenv
$ npm install -D typescript ts-node @types/node @types/express nodemon
$ tsc --init
$ mkdir src dist

tsconfig.json

{
  "compilerOptions": {
    ...
    "outDir": "./dist", 
    ...
  },
  "include": ["src/**/*"],
  "exclude": ["node_modules"] 
}

src/app.ts

// https://blog.logrocket.com/how-to-set-up-node-typescript-express/
import express, { Express, Request, Response } from 'express';
import dotenv from 'dotenv';

dotenv.config();

const app: Express = express();
const port = process.env.PORT;

app.get('/', (req: Request, res: Response) => {
  res.send('Express + TypeScript Server');
});

app.listen(port, () => {
  console.log(`⚡️[server]: Server is running at http://localhost:${port}`);
});

package.json

  ...
  "scripts": {
    "build": "tsc",
    "start": "node dist/app.js",
    "dev": "nodemon --exec ts-node src/app.ts"
  },
  ...

.env

.gitignore

참고)

0개의 댓글