Node 치면 들어갈 수 없음. console.log 가능.
ctrl + c 두 번 누르면 나갈 수 있음.
import export -> es6 문법
commonJS 문법이라고 하는 commonjs module system에서 따온 export and require
404 - not found( 클라이언트)
500 대는 서버문제
127.0.0.1 = localhost 란 뜻(내 컴퓨터를 의미)
-express 하면 구글검색시 그 내용들은 제외되고 출력
채터박스 server 먼저 따라해보기
require 노드 모듈을 요청하는 것
서버가 띄워진 상태에서 새 탭을 열어서 npm test 를 돌려야 함
포트 닫기, 어떻게 포트를 강제로 열고 닫는지 알아보기.
JS 인터프리터
이벤트 루트란 개념?
논블럭킹 I/O?
REPL(Read, Eval, Print, Loop)
오늘 할려는게 REST API 서버
웹소켓은 실시간으로 통신 가능
LIVE-SERVER(recast에서 자동 리프레쉬 되었던)
모듈(https://jongmin92.github.io/2016/08/25/Node/module-exports_exports/)
관련된 코드들을 하나의 코드로 캡슐화하는 것을 말함.
모듈 추출하기(exporting)
/ greetings.js
sayHelloInEnglish = function() {
return "Hello";
};
sayHelloInSpanish = function() {
return "Hola";
};모듈 추출의 3가지 방법
1. greeting.js 첫 부분에 다음과 같은 코드 추가
var exports = module.exports = {}
2. 다른 파일에서 exports 객체 사용을 원할 때,
exports.sayHelloInEnglish = function() {
return "HELLO";
};
exports.sayHelloInSpanish = function() {
return "Hola";
};
3. 아래와 같이 대체할 수도 있음. 왜냐하면 exports와 module.exports는 같은 객체를 참조하기 때문.
module.exports = {
sayHelloInEnglish: function() {
return "HELLO";
},
sayHelloInSpanish: function() {
return "Hola";
}
};모듈 사용하기
require -> node.js에서 module을 import(추가)해주기 위해 사용
//require는 다음과 같이 정의
var require = function(path) {
return module.exports;
}
main.js에서 greetings.js를 require 합니다.
// main.js
var greetings = require("./greetings.js");
// 위 코드는 아래 코드와 동일한 코드다.
// main.js
var greetings = {
sayHelloInEnglish: function() {
return "HELLO";
},
sayHelloInSpanish: function() {
return "Hola";
}
};
// main.js 에서 greeting.js 의 값과 메소드에 접근할 수 있습니다.
// main.js
var greetings = require("./greetings.js");
// "Hello"
greetings.sayHelloInEnglish();
// "Hola"
greetings.sayHelloInSpanish();require 키워드는 object를 반환. module.exports와 export는 call by reference로 동일한 객체를 바라보고 있고, 리턴되는 값은 항상 module.exports 이다. 모듈은 기본적으로 객체이고, 이 객체를 module.exports, exports 모두 바라보고 있는데, 최종적으로 return 되는 것은 무조건 module.exports 라는 것입니다.
var express = require('express');
var router = express.Router();
/* GET home page. */
router.get('/', function(req, res) {
res.render('index', { title: 'Express' });
});
module.exports = router;
---
위의 소스는 다음과 같이 해석할 수 있습니다.
express.Router() 가 리턴한 "객체"에 일부 프로퍼티를 수정한 뒤, 이
객체 자체를 모듈로 리턴한 것입니다.require()
module.exports 내가 이걸 다른데서 쓰겠다고 정의
노드는 커먼 js 문법을 사용중
import, export 바벨로 변환하면 이렇게 됨
비구조 할당 연습해서 익숙해질 것!
./ = current directory
require(‘underscore’) -> 이 경우는 디펜던시들을 node_modules에서 받아옴. 이름만 써졍씨는 경우.
npm install underscore 이 방식으로 인스톨함
공식 API 문서를 보는 습관을 들일 것. 익숙해져야 함. nodejs.org
package.json에서 모듈 빌드하는 거 알아보기
npm run (원랜 npm run test 인데, start, test, build는 npm run을 생략할 수 있음)
npm start
요새는 npm install
npm i 만 해도 save가 자동으로 됨
npm unstall 하면 package.json에서 없어짐
yarn
sudo
패키지가 module.export를 잘 안 한 경우, modluel import가 안 됨
디버깅 노드 중요!!
node server/basic-sever.js === npm start
node — inspect server/basic-sever.js === npm start 크롬 브라우저에서 볼 수 있음
chrome://inspect/#devices
CORS가 무슨 의미인지?
http://127.0.0.1:3000/classes/messages?
file을 써서 특정 json을 써서 저장하게?
git commit —no-verify
싱글 스레드는 하나의 콜스택을 가진다는 이야기
web APIs
에러 찍힐때 콜스택 순서에 따라 나옴.
은 클라이언트인 척 할 수 있다. client 측 문제일 가능성을 봉쇄해버리고, 서버 문제만 해결 하기 좋다.
전제는 내 서버가 켜져있을때 데이터를 날릴 수 있다.
router를 만들어서 API를 핸들링 하는 애들을 늘려갈 수 있다. express를 쓰면 더 편리하게 할 수 있다.
exports로 3개의 함수를 내보내고 있다.
JSON.stringfy(data) -> data가 없으면 null
// vs에서 커맨드 + 함수명을 누르면 코드로 넘어갈 수 있음
request.url.includes로 체크할 것. 왜냐하면 (messages/는 먹히지 않아버린다!) 작은 오차도
{ message : messages } <- stringfy 시켰기 때문에 갈 수 있다!
// preFlighted 브라우저가 먼저 날려봄.
// status에서 200번대는 다 ok의 종류다.
// request를 날리기 전에 options를 먼저 보내서, POST를 보낼 수 있는지, 어떤지 중재를 할 수 있다.module.export parts 부분.
json.stringify의 정의와 parse. 언제 json을 하고 언제 parse를 해야 할지 정리해주자.
module.exports.requesthandler = requesthandler;