node, express, nestjs

Aug 7, 2021 - nodejs express setup~mongoDB~git에 연결하기 (SSH)

jongviet 2021. 8. 7. 03:34

*8월7일

 

*node.js

-express.js = framework

 

디렉토리 설정 후

npm init

대부분의 값 default로 엔터

author : Ki

 

index.js 생성 (백엔드 시작점)

express js 설치

npm install express --save

 

node_module이라는 폴더안에 dependencies들이 관리됨

 

index.js 파일 내 공식 document 내용 가져와서 붙임


const express = require('express')   //express 모듈 가져옴;

const app = express() //새로운 express 앱 생성

const port = 5000

 

app.get('/', (req, res) => {   //root directory 처리

  res.send('Hello World!')

})

 

app.listen(port, () => {

  console.log(`Example app listening at http://localhost:${port}`)

})


 

package.json scripts 부분에

 

"start" : "node index.js" 처리

 

터미널에서 npm run start

 

서버종료는 ctrl + c

 

내용 변경 시 서버 재구동 필요

 

 

*mongo DB cluster 생성 및 user 생성

 

mongodb+srv://아이디:비밀번호@클러스터명.3gnqm.mongodb.net/myFirstDatabase?retryWrites=true&w=majority

 

접근 아이피에 본인 아이피 추가

터미널 mongoose 다운로드

npm install mongoose --save


const mongoose = require('mongoose')

mongoose.connect('mongodb+srv://아이디:비밀번호@클러스터명.3gnqm.mongodb.net/myFirstDatabase?retryWrites=true&w=majority', {

  useNewUrlParser : true, useUnifiedTopology: true, useCreateIndex: true, useFindAndModify: false

}).then(() => console.log('mongoDB Connected'))

  .catch(err => console.log(err))


 

*MongoDB model & schema

-model은 schema를 감싸는 역할을하고, schema는 문서의 구조, 기본값, 검증 등을 정의함. 

-model은 db에 접근할 수 있는 인터페이스를 제공하고 이를 통해 레코드를 생성, 셀렉, 업데이트, 삭제할 수 있음. DAO 느낌!


 

*git

 

window 버전 git 인스톨

새로운 아이디 생성

 

git config --global user.name "ki"

git config --global user.email "jongki6161@gmail.com"

git config --list

 

 

root 위치에 git 저장소 만들어야 함

git init // 새 저장소 생성

git status // commit 처리안된 파일 리스트

 

 

working directory 로컬 작업 중 -> git add 처리 후, staging area(대기소)로 이동 -> git commit 후 git repository(local)로 이동 -> git push 후, git repository(remote)로 이동

 

git add .

git status // staging area 리스트 조회용

 

node_modules(라이브러리 파일) 는 깃 저장소에 올리지않아도 됨!!

 

.gitignore 파일에 node_moduels 넣은 후,

 

git rm --cached node_modules -r  //로 add된 node_modules 리스트 제거

 

git commit -m "init"   //m은 메시지 -> local git에 올라갔음~

 

local - staging -> git local -> git remote

*github

-git은 로컬 툴, github은 클라우드 서비스

-안전하게 연결하려면 SSH 사용해야함; secure shell;

 

$ ls -a ~/.ssh         //ssh 연결 여부 확인 가능

 

신규 생성은 'git ssh' 구글에 검색하여,

 

Generating a new SSH key and adding it to the ssh-agent 진행

 

윈도우 기준

 

git bash

 

$ ssh-keygen -t ed25519 -C "your_email@example.com" //본인 이메일

Generating public/private ed25519 key pair

 

ssh-agent 열고, private key 추가

 

eval "$(ssh-agent -s)"

$ ssh-add ~/.ssh/id_ed25519

 

Adding a new SSH key to your GitHub account

 

$ clip < ~/.ssh/id_ed25519.pub //ssh public key 복사

 

깃헙 - setting - ssh and gpg keys - title + 복사한 public key 정보 입력 후 add ssh key

 

vs code 터미널 상

 

git remote add origin https://github.com/id/repository.git

git branch -M main

git push -u origin main

 

git remote 연결 및 push 완료