on
nodejs에 공리 사용
nodejs에 공리 사용
axios는 브라우저 및 node.js에 대한 약속 기반 Http 클라이언트입니다.
아래 명령을 사용하여 nodejs 프로젝트에 axios를 설치합니다.
js npm install axios
아래 문을 사용하여 공리를 가져옵니다. ```js const axios = require('axios'); ``` 아래 샘플 코드에서는 공리 사용 방법을 보여 줍니다. axios는 약속 개체 핸들 성공 및 오류 데이터를 반환하므로, 이때 collback 함수와 catch() 콜백 함수를 사용합니다. ```js app.get("/yourapi", function(req, res, next) => { axios.get("https://replace/your/url/here") .then(function (response) { // handle success return res.send(response.data); }) .catch(function (error) { // handle error console.log(error); // return res.send(error["message"]); // send response or next(error); // pass error to global error handler }) }) ``` 전역 오류 처리기 예. 항목 스크립트 파일(index/server.js 파일) 끝에 오류 처리기 미들웨어를 사용해야 합니다. ```js app.use(function (err, req, res, next) { res.status(500).send(err["message"); }) ``` 참고문헌 - 공리 - 오류 처리기
from http://gong-tech.tistory.com/31 by ccl(A) rewrite - 2021-09-24 01:26:47