1. Express

Node.js Framework Express

Node.js 의 λŒ€ν‘œμ μΈ μ›Ήν”„λ ˆμž„μ›Œν¬μΈ Express λ₯Ό ν™œμš©ν•˜μ—¬ κ°„λ‹¨ν•œ μ„œλ²„λ₯Ό λ§Œλ“€μ–΄λ³Έλ‹€.

ν”„λ‘œμ νŠΈ 폴더 λ§Œλ“€κ³ , ν•΄λ‹Ή 폴더에 μ§„μž…ν•˜κΈ°

mkdir express-demo-app
cd mkdir express-demo-app

npm μ΄ˆκΈ°ν™”

npm init -y

.gitignore 파일 생성

touch .gitignore

파일 생성 ν›„ .gitignore νŽ˜μ΄μ§€μ—μ„œ VisualStudioCode, node ν‚€μ›Œλ“œ μž…λ ₯ ν›„ copy&paste ν•œλ‹€. node_modules, dist κ°€ λ°˜λ“œμ‹œ μ‘΄μž¬ν•΄μ•Ό ν•œλ‹€.

μ•„λž˜μ˜ λͺ¨λ“ˆ μ„€μΉ˜

npm i -D typescript
npm i -D ts-node
npm i -D nodemon
npm i eslint
npm i express
npm i @types/express

nodemon 은 개발 νŽΈμ˜μƒ μ„€μΉ˜. ts-node 에 λŒ€ν•˜μ—¬ μ˜μ‘΄μ„±μ„ κ°€μ§€κ³  있기 λ•Œλ¬Έμ— ts-node κ°€ λ°˜λ“œμ‹œ μ„€μΉ˜λ˜μ–΄μžˆμ–΄μ•Ό ν•œλ‹€.

각쒅 μ΄ˆκΈ°ν™”

# tsconfig.json 파일 생성
npx tsc --init
# lint μ„€μ •. .eslintrc.js 파일 생성
npx eslint --init
lint-init-for-node.png

Prettier κ°€ extension 으둜 μ„€μΉ˜λ˜μ–΄ μžˆλŠ” 경우, lint μ„€μ • ν›„ 좩돌 ν”Όν•˜κΈ°

npm install --save-dev eslint-config-prettier
// .eslintrc.js
...
overrides: [
  {
    env: {
      node: true,
    },
    files: ['.eslintrc.{js,cjs}'],
    parserOptions: {
      sourceType: 'script',
    },
  },
  {
    extends: ['xo-typescript', 'prettier'], // <- Add here!
    files: ['*.ts', '*.tsx'],
  },
],

ts-node, nodemon μ‹€ν–‰ 방법

npx ts-node
npx nodemon <path>

package.json μˆ˜μ •

...
"main": "app.ts",
"scripts": {
  "start": "nodemon app.ts",
  "lint": "eslint --fix .",
  "test": "echo \"Error: no test specified\" && exit 1"
},

app.ts 파일 생성

루트 νŒ¨μŠ€μ— app.ts 파일 생성 ν›„ μ•„λž˜μ™€ 같이 μž‘μ„±

import express from 'express';
import cors from 'cors';

const port = 3000;
const app = express();

app.use(cors());
app.get('/', (req, res) => res.send('Hello, World!'));

app.listen(port, () => {
  console.log(`Server running at http://localhost:${port}`);
});

REST URL κΈ°λ³Έ κ·œμΉ™

GET /products
GET /products/{id}
POST /products
PATCH /product/{id}
DELETE /product/{id}

μ‹€ν–‰

npm run start

TO BE ADDED

  • How to set cors for other origin

  • How to set url path and divide api

Last updated