오도원입니다.

건강과 행복을 위하여

반응형

프로젝트/니랑내랑 17

REST API 5. MongoDB 연결하기

저기에 나온 url이 nodejs에서 mongodb 데이터베이스와 연결할 때 필요한 mongodb url이다. root는 user이름이고, password는 cluster를 생성할 때 등록한 비밀번호이다. Nodejs 코드 작성하기 이제 nodejs에서 코드를 작성해서 mongodb와 연결해보자. const mongoose = require('mongoose'); const MONGO_URL = [복사한 URL 주소] // 부분은 실제 password로 바꿔야한다. mongoose.connect(MONGODB_URL ...생략...

REST API 2. POST method, postman, JSON viewer 사용

1. JSON viewer json viewer chrome extenstion으로 다운받으면 된다. 2. postman 우리가 www.google.com이라는 url로 접근하는 것은 구글의 GET method를 통해 들어가는 것이다. POST를 하기 위해서는 직접적으로는 할 수 없다. 이것을 간편하게 보기위해 postman 어플리케이션을 사용하면된다. postman을 사용하면 GET, POST, PUT, DETETE를 통해서 송수신 되는 json 객체들을 쉽게 관찰할 수 있다. 3. POST method const express = require('express'); const server = express(); const bodyParser = require('body-parser'); server...

REST API 1. REST API란?

https://github.com/ohdowon064/REST_API/tree/dc48c1ac421b5b8bbf7dbf653b49c8175441311a ohdowon064/REST_API for study rest api. Contribute to ohdowon064/REST_API development by creating an account on GitHub. github.com https://www.youtube.com/watch?v=HjWYK_ORW0w&list=PLHGvDasahwZNIJ0aZQIhrf1Tg7Djqk7VQ&index=1 다음을 공부하여 정리하기 위해 작성했다. 1. REST API Representational State Transfer의 약자로서, HTTP 기반으로 필요한 자원..

Express 10. 라우터 분리하기

https://github.com/ohdowon064/Node.js ohdowon064/Node.js for nodejs study. Contribute to ohdowon064/Node.js development by creating an account on GitHub. github.com 조금씩 nodejs가 이해되기 시작한다. 생활코딩 정말 추천한다. 라우터 분리 전 전체 코드 기존의 코드를 살펴보자. const express = require('express'); const app = express(); // express()함수는 application 객체를 반환한다. const fs = require('fs'); // file system 모듈 const template = require('..

Express 9. Error Handling

https://expressjs.com/en/guide/error-handling.html Express error handling Error Handling Error Handling refers to how Express catches and processes errors that occur both synchronously and asynchronously. Express comes with a default error handler so you don’t need to write your own to get started. Catching Errors It’s important expressjs.com 코드 마지막에 에러를 처리하는 코드를 추가한다. app.use((req, res, next) =..

Express 8. 정적인 파일 서비스

정적인 파일(static file)로 이미지, 자바스크립트, css 파일등이 있다. 이미지 파일을 다운로드 받을 수 있는 unsplash라는 좋은 사이트가 있다. static file을 서비스할 때는 정적인 파일을 서비스하고자 하는 디렉토리를 직접 지정하면 된다. app.use(express.static('public')); 이렇게 미들웨어를 등록하면 public 디렉토리 내의 파일, 디렉토리를 url을 통해 접근할 수 있다. app.get('/', (req, res) => { console.log('req.list => ', req.list); var title = 'Welcom home!'; var description = 'Hello, Node.js'; var list = template.list(..

Express 6. 미들웨어 사용하기 compression

Express에 body-parser 사이트 전체의 글을 복사해서 3번 붙여쓴 다음 제출하여 글을 생성했다. 구글의 검사 항목으로 들어가서 Network 카테고리에서 Express글의 데이터 크기를 보면 46.7KB로 매우 큰 것을 확인할 수 있다. 이것을 compression 미들웨어를 통해서 압축을 하여 해결해보자. const compression = require('compression'); app.use(compression()); 매우 간단하다. compression() 함수를 실행하면 미들웨어가 만들어지고 app.use를 통해서 장착된다. 그러면 미들웨어가 등록된 것이다. 이제 다시 데이터를 확인해보자. windows에서 ctrl + shift + R을 통해 강제 새로고침이 가능하다. 데이터의..

반응형