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
{
"compilerOptions": {
...
"outDir": "./dist",
...
},
"include": ["src/**/*"],
"exclude": ["node_modules"]
}
// 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}`);
});
...
"scripts": {
"build": "tsc",
"start": "node dist/app.js",
"dev": "nodemon --exec ts-node src/app.ts"
},
...
참고)