Be well coding/In Immersive
6/18 / server
be well
2019. 6. 18. 19:47
SA 3 리액트 props 전달하기. 화면 렌더링
props를 통해 data 를 하위 컴포넌트에 전달하는 문제.
mini server SPRINT
const http = require('http');
const PORT = 5000;
const ip = 'localhost';
내 노트북을 미니 서버로.. 사용?해보는 미니 스프린트.
const server = http.createServer((request, response) => {
let headers = defaultCorsHeader;
// request.setEncoding('utf-8')
let body = '';
request.on('data',(chunk)=>{
body += chunk
})
request.on('end',()=>{
if(request.method==="OPTIONS"){
response.writeHead(200, headers);
response.end()
}
if(request.method==='POST'){
if(request.url==='/lower'){
response.end(JSON.stringify(body.toLowerCase()));
}
else if(request.url==='/upper'){
response.end(JSON.stringify(body.toUpperCase()));
}
} else {
response.end('that\' no no')
}
})
});
server.listen(PORT, ip, () => {
console.log(`http server listen on ${ip}:${PORT}`);
});
const defaultCorsHeader = {
'access-control-allow-origin': '*',
'access-control-allow-methods': 'GET, POST, PUT, DELETE, OPTIONS',
'access-control-allow-headers': 'content-type, accept',
'access-control-max-age': 10
};
기본적으로 서버는 request 와 response 를 가지고 있다.
클라이언트에서의 요청을 response.writeHead 로 허용가능한 지 판단하고 각 요청에 따라 알 맞은 응답을 준다.
body 라는 빈 공간에 data 를 chunk 라는 개념으로 쪼개서 들어온 것을 다시 합쳐주고 stringify 와 parse 를 이용해 가공하여
클라이언트에게 응답을 준다.
서버... node server/ 파일
콘솔이 터미널에 찍히는거라고는 상상을 못 했다... 넘나 어려운 것....