[서버 배포] 서버의 역할, mongoDB 아틀라스

AnSuebin·2022년 9월 4일
0

서버의 역할

  • 유저가 접속하는 url에 따라서 html 화면 제공
    => server의 index.js에 '*'모든 영역에서 html 반환
app.get('*', (req, res) => {
  res.sendFile(path.join(__dirname, '../PostSite/build/index.html'))
})
  1. mongoDB 활용하기
  • mongoose 깔기 (mongoDB 편하게 쓸 수 있는 라이브러리)
npm i mongoose --save
// 몽구스 가져와서
const mongoose = require('mongoose');

// 서버 열 때, 몽구스 적용 하도록 복붙
app.listen(port, () => {
  mongoose.connect('mongodb://localhost:27017/test');
  console.log(`Example app listening on port ${port}`)
})
  • 내 서버로 변경
app.listen(port, () => {
  mongoose.connect('이부분 내 api 주소 가져온 것으로 변경');
  console.log(`Example app listening on port ${port}`)
})
  • 에러를 감지하기 위해 then 과 catch 메소드 설정
app.use(express.static(path.join(__dirname, '../PostSite/build')))
// 서버를 열고, 내주소로 변경
app.listen(port, () => {
  mongoose.connect(
    '내 api 서버'
    // 실행이 잘 되었을 때
  ).then (()=>{
    console.log(`Example app listening on port ${port}`)
    console.log('Connecting MongoDB...')
    // 실행이 잘 안되었을 때
  }).catch((err)=>{
    console.log(`${err}`)
  })
})
profile
고객에게 명료한 의미를 전달하고, 명료한 코드를 통해 생산성 향상에 기여하고자 노력합니다.

0개의 댓글