expreejs [x-clone]

조하빈 ·2023년 11월 30일
0

기본 세팅

  • 터미널 명령어
  1. git init
  2. npm init -y (여기서 -y는 모두 yes로 절차 생략할때 씀) 이후 작가나 , 저작권정도만 수정
  3. readme.md와 gitignore 생성 후 gitignore에 .env , dist, node_modules 입력
  4. npm i express로 express 설치
  5. npm i -D typescript @types/express @types/node 타입스크립트 명령어 입력
  6. npx tsc -- init => npx tsc(index.ts를 index.js로 변환) => node index.js(index.js를 실행하는 노드명령어) 입력으로 서버 실행
  • 하지만 이 방법은 라우팅, 파일변환을 계속 해줘야되므로 비효율적임
    고로 다음방법을 이용
    6-1.tsconfig.json 파일 58번째줄 outDir를 dist파일로 변경
    6-2.package.json 파일 scripts부분을
    "build": "npx tsc", "start": "node index.js" 입력해줌
    6-3 npm i -D nodemon ts-node 명령어 입력
    6-4 다시 패키지 파일로가서 "dev": "nodemon --watch \"*.ts\" --exec \"ts-node\" index.ts" 입력
    이 방법을 거치면 바로바로 바뀌어서 나옴
    7.npm i -D prisma
    8.npm i @prisma/client 명령어 입력
    9.npx prisma init
    10.planetscale에서 db생성 후 npx prisma db push 후 npx prisma studio하면 db테이블 확인 가능

라우터 생성


import express from "express";

const router = express.Router();

router.post("/", async (req, res) => {
  try {
    console.log(req.body);
    return res.json({
      ok: true,
    });
  } catch (error) {
    console.log(error);

    return res.status(500).json({
      ok: false,
      message: "Server error.",
    });
  }
});

export default router;
profile
PPisland

0개의 댓글