개발일지/팀GC
05/28 node.js 서버구축연습 1
박준희
2021. 5. 28. 18:07
728x90
Hello World! 출력
1. workspace생성
\workspace\nodejs\helloworld
2. app.js 생성
app.js
let http = require('http');
//request, response
let server = http.createServer((req, res)=>{
res.write("hello world!");
res.end();
});
server.listen(8080, ()=>{
console.log("서버 시작되었습니다 포트: 8080");
});
서버 json파일 생성
npm init
3. node.js 서버 실행
visual studio Code의 터미널에서 코드가 있는 디렉토리로 이동
cd C:\Users\TJOEUN\Documents\workspace\nodejs\helloworld
서버 실행
node app
4. 클라이언트에서 요청
브라우저에서 주소 입력
http://localhost:8080/
메서드 전송 방식
Get,
Post
클라이언트에서 Get 방식으로 데이터를 전송 했을때
서버에서 해당 데이터를 받아 출력 하고
클라이언트에게는 잘받았다고 응답을 해주는 코드를 작성하세요
statuscode : 200
http://localhost:8080/?username='hong'&password='1234'
(Query, QueryString)
username : 'hong'
password: '1234'
200
1. get방식
app.js
let http = require('http');
let url = require('url');
//request, response
let server = http.createServer((req, res)=>{
let _url = req.url;
let query = url.parse(_url,true).query;
if(req.method == 'GET')
{
console.log("username : " + query.username + ", password : " + query.password);
}
res.end("status: " + res.statusCode);
});
server.listen(8080, ()=>{
console.log("서버 시작되었습니다 포트: 8080");
});
728x90