Skip to content

Instantly share code, notes, and snippets.

@shoveller
Last active April 4, 2018 04:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shoveller/9cc8c482b3fd29dd41714523395975ba to your computer and use it in GitHub Desktop.
Save shoveller/9cc8c482b3fd29dd41714523395975ba to your computer and use it in GitHub Desktop.
전통적인 http 서버의 구현 예
const http = require('http');
const url = require('url');
const querystring = require('querystring');
const onRequest = (res, method, pathname, params) => res.end(JSON.stringify(params));
http.createServer((req, res) => {
const method = req.method;
const uri = url.parse(req.url, true);
const pathname = uri.pathname;
if (method === 'POST' || method === 'PUT') {
let body = '';
req.on('data', data => body += data);
req.on('end', () => {
let params = '';
if (req.headers['content-type'] === 'application/json') {
params = JSON.parse(body);
} else {
params = querystring.parse(body);
}
onRequest(res, method, pathname, params);
})
} else {
onRequest(res, method, pathname, uri.query);
}
}).listen(8000);
@shoveller
Copy link
Author

간결하긴 하지만 하나의 함수에 로직이 많이 들어가서 가독성이 떨어진다.
규모가 커져도 계속 이런 식으로 서버를 작성해야만 할까?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment