오도원입니다.

건강과 행복을 위하여

반응형

Nodejs 18

Nodejs. axios를 활용한 http 요청

axios로 특정 url 위치의 데이터를 받아오는 방법에 대해서 알아보자. router.get('/', async(req, res) => { res.json(); }) 위의 라우트에서 특정 url 데이터를 받아오고 해당 데이터를 json으로 전송하고자 한다. try { var ret = await axios.get('http://[api url]'); }catch(err) { console.log(err); } 받아온 response객체가 ret변수에 저장된다. router.get('/', async (req, res, next) => { try{ var ret = await axios.get('http://[api url]'); }catch(err){ console.log(err); } console...

Development/Node.js 2020.08.18

about package.json

애플리케이션을 개발할 때 package.json 파일을 이용하면 사용하는 확장 모듈에 대해 의존성 관리가 가능해서 편하다. package.json 파일은 기본적으로 CommonJS 명세를 따르며 JSON 형식 파일이다. 직접 작성할 수도 있고, npm init 명령어를 통해 자동으로 생성할 수 있다. 해당 애플리케이션을 위한 확장 모듈 정보는 npm install -save를 통해 모듈 정보를 추가할 수 있다. npm init npm install -save 1. 예시 { "name" : "test", "description" : "javascript's test programming.", "keywords" : ["util", "f", "server", "client", "browser"], "auth..

Development/Node.js 2020.07.29

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(..

반응형