deploy

hrj2233·2021년 11월 24일
0

deploy

목록 보기
1/1

heroku로 배포할건데
어떤 node.js 환경에서도 서버가 실행될 수 있게 설정을 바꾸고
db도 설정을 바꾸고
파일들은 아마존에 올릴거임.

프론트 빌드하기
바꿔주고

"dev:assets": "webpack --mode=development -w"

생성해주고

"build:assets": "webpack --mode=production",

백엔드 빌드하기

babel-node는 개발할때, babel-cli는 배포할때 사용.

npm install --save-dev @babel/core @babel/cli

아래와 같이 작성하면 src 폴더안의 내용을 babel해서 build라는 디렉토리에 src안에 있는 파일들의 코드를 호환성 있는 코드로 바꿔놓음.

"build:server": "babel src -d build --ignore src/client",

변환된 자바스크립 코드니까 node는 babel 없이도 이해할 수 있음.

"start": "node build/init.js",

간편하게 빌드 둘다 합치기

"build": "npm run build:assets && npm run build:server",

heroku 배포
heroku가 보는 모든 파일은 github가 보는 파일.
heroku가 port변수를 줌.

process.env.PORT

mongodb
mongodb에서 프로젝트 만들고 클러스터 만들기.

github
개발자 설정에서 테스트 랑 실제 앱 구동할 수 있도록 두개 만들기.

aws
1. s3들어가서 만들기.
그 다음에 권한에 들어가서 퍼블릭 엑세스에서 마지막 2개 체크표시.

  1. api key 만들기.
    그래야 node.js 코드가 aws와 이야기 할 수 있음.
    IAM 들어가기.
    사용자 만들고 프로그래밍 방식 액세스키 체크표시
    권한 들어간 다음에 amazons3fullaccess 체크표시
    -> amazons3fullaccess는 s3에서 할 수 있는 모든 권한을 주는거.파일에 대해서만.
    완료하면 액세스 키 ID, 비밀 액세스 키 주어짐. 한번밖에 안보여주니까 기억해야하고 heroku 변수에 적용.

  2. multer s3 패키지 다운로드.
    multers3를 통해 웹사이트에 업로드 하는 대신 aws에 업로드 할거임.
    aws-sdk도 다운로드 해야함.

import multer from 'multer';
import multerS3 from 'multer-s3';
import aws from 'aws-sdk';

const s3 = new aws.S3({
  credentials: {
    accessKeyId: process.env.AWS_ID,
    secretAccessKey: process.env.AWS_SECRET,
  },
});

// heroku로 배포하면 NODE_ENV는 히로쿠에 정의되어 있음.
// NODE_ENV는 heroku에서 production과 같음.
const isHeroku = process.env.NODE_ENV === 'production';

const s3ImageUploader = multerS3({
  s3: s3,
  bucket: '버킷이름/images',
  acl: 'public-read',
  contentType: multerS3.AUTO_CONTENT_TYPE,
});

const s3VideoUploader = multerS3({
  s3: s3,
  bucket: '버킷이름/videos',
  acl: 'public-read',
  contentType: multerS3.AUTO_CONTENT_TYPE,
});

export const localsMiddleware = (req, res, next) => {
  res.locals.loggedIn = Boolean(req.session.loggedIn);
  res.locals.siteName = 'VSA';
  // {} 표시는 empty를 나타낸 거임.
  res.locals.loggedInUser = req.session.user || {};
  res.locals.isHeroku = isHeroku;

  next();
};

export const protectorMiddleware = (req, res, next) => {
  if (req.session.loggedIn) {
    return next();
  } else {
    req.flash('error', 'Log in first.');
    return res.redirect('/login');
  }
};

export const publicOnlyMiddleware = (req, res, next) => {
  if (!req.session.loggedIn) {
    return next();
  } else {
    req.flash('error', 'Not authorized');
    return res.redirect('/');
  }
};

export const avatarUpload = multer({
  dest: 'uploads/avatars/',
  limits: {
    fileSize: 3000000,
  },
  // heroku면 aws에 저장, 아니면 일반폴더 storage 사용
  storage: isHeroku ? s3ImageUploader : undefined,
});

export const videoUpload = multer({
  dest: 'uploads/videos/',
  limits: {
    fileSize: 10000000,
  },
  // heroku면 aws에 저장, 아니면 일반폴더 storage 사용
  storage: isHeroku ? s3VideoUploader : undefined,
});

0개의 댓글