main.js 8.27 KB

var http = require('http');
var fs = require('fs');
var url = require('url');
var qs = require('querystring');
var template = require('./lib/template.js');
/*
const style={
    backgrondColor = 'blue',
    border:'1px solid border'
    
}*/

const { count } = require('console');

var app = http.createServer(function(request,response){
    var _url = request.url;
    var queryData = url.parse(_url, true).query;
    var pathname = url.parse(_url, true).pathname;
    if(pathname === '/'){
      if(queryData.id === undefined){
          var title = '소개';
          var description = '개개인의 신체 정보에 따라 일일 권장 섭취량을 알려주고, 좋아하는 음식을 검색하면 영양정보를 알려주는 사이트 입니다.';
          var html = template.html(title,`
          <br><br><br><br>
          <style>
            @import url('https://fonts.googleapis.com/css2?family=Jua&family=Noto+Sans+KR&display=swap');
          </style>
          <h2 style="font-family: 'Jua', sans-serif;"><font color="red"><text align:center>${title}</font></h2>
          <p style="font-family: 'Jua', sans-serif;">${description}</p>` );
          
          response.writeHead(200);
          response.end(html);
      }
    }
    else if(pathname === '/countKcal'){
        var title = '칼로리 계산';
        var description = '성별, 나이, 키, 몸무게, 활동량을 입력하시면 일일 권장 섭취량(kcal)를 알려드립니다!';
        var html = template.html(title,`
        <h2>${title}</h2>
        <p>${description}</p>
		<form action="/countKcal_process" method="post" >
			성별 :<br />
			<select name="gender">
				<option value="man">남자</option>
				<option value="woman">여자</option></select
            ><br />
            나이(세) : <br />
            <input type="text" name="age" /><br />
			키(cm) : <br />
			<input type="text" name="height" /><br />
			몸무게(kg) : <br /><input type="text" name="weight" /><br />
			활동량 :<br />
			<select name="activity">
				<option value="low">앉아 있거나 가벼운 활동(운동을 거의 또는 전혀하지 않음)</option>
				<option value="mid">활동적 또는 보통 활동(건설노동자 또는 매일 1시간씩 달림)</option>
				<option value="high">많은 활동(농업노동자 또는 매일 2시간동안 수영을 함)</option>
				</select
			><br />
			<input type="submit" value="계산하기" />
		</form>` );
        response.writeHead(200);
        response.end(html);

     
    }
    else if(pathname === '/countKcal_process'){
        var body = '';
        request.on('data', function(data){
            body = body + data;
        });
        request.on('end', function(){
            var post = qs.parse(body);
            var title = '칼로리 계산';
            var description = '성별, 키, 몸무게, 활동량을 입력하시면 표준체중, 현재 체중을 유지하는데 필요한 열량, 다이어트 열량을 알려드립니다!';
            var gender = post.gender;
            var age = post.age;
            var height = post.height;
            var weight = post.weight;
            var BMR; //기초대사량(bmr) Harris-BenedictEquation(B.E.E) 방법
            var BMI = (weight/(height*height)*10000).toFixed(1); //비만도
            var BMI_msg = '';
            if(BMI >= 30){
                BMI_msg += '고도비만';
            }
            else if(BMI >=25 && BMI <30){
                BMI_msg += '비만';
            }
            else if(BMI >=23 && BMI <25){
                BMI_msg += '과체중';
            }
            else if(BMI >=18.5 && BMI <23){
                BMI_msg += '정상';
            }
            else if(BMI >= 0 && BMI < 18.5){
                BMI_msg += '저체중';
            }
            else {
                BMI_msg += "Error";
            }
            var gender_default = '';
            if(gender == "man"){
                gender_default =
               `<option value="man" selected>남자</option>
                <option value="woman">여자</option>`;
                BMR = (66.5 + 13.75*weight + 5.003*height - 6.755*age).toFixed(1);
            }
            else {
                gender_default =
                `<option value="man" >남자</option>
                <option value="woman" selected>여자</option>`;
                BMR = (655. + 9.563*weight + 1.85*height - 4.676*age).toFixed(1);
            }
            var std_weight = (height*height/10000*((gender =="man") ? 22 : 21)).toFixed(1); //남자면 22, 여자면 21을 곱함.
            var activity = post.activity;
            var activity_idx;
            var TDEE; //Total Daily Energy Expenditure 현재 체중을 유지하기 위한 열량
            var activity_default = '';
            var weight_gap = (weight - std_weight).toFixed(1);
            var weightMsg = "";
            if(weight_gap > 0){
                weightMsg += `표준 체중보다 ${weight_gap}kg 많습니다.`
            }
            else if(weight_gap < 0){
                weightMsg += `표준 체중보다 ${-weight_gap}kg 적습니다.`
            }
            else {
                weightMsg += '표준 체중과 몸무게가 같습니다.'
            }
            switch(activity){
                case "low":
                    activity_idx = 1.53;
                    TDEE = (BMR*activity_idx).toFixed(1);
                    activity_default = `
                    <option value="low" selected>앉아 있거나 가벼운 활동(운동을 거의 또는 전혀하지 않음)</option>
                    <option value="mid">활동적 또는 보통 활동(건설노동자 또는 매일 1시간씩 달림)</option>
                    <option value="high">활발히 활동(농업노동자 또는 매일 2시간동안 수영을 함)</option>
                    </select>
                   ` 
                    break;
                case "mid":
                    activity_idx = 1.76;
                    TDEE = (BMR*activity_idx).toFixed(1);
                    activity_default = `
                    <option value="low" >앉아 있거나 가벼운 활동(운동을 거의 또는 전혀하지 않음)</option>
                    <option value="mid" selected>활동적 또는 보통 활동(건설노동자 또는 매일 1시간씩 달림)</option>
                    <option value="high">많은 활동(농업노동자 또는 매일 2시간동안 수영을 함)</option>
                    </select>
                   ` 
                    break;
                default:
                    activity_idx = 2.25;
                    TDEE = (BMR*activity_idx).toFixed(1);
                    activity_default = `
                    <option value="low" >앉아 있거나 가벼운 활동(운동을 거의 또는 전혀하지 않음)</option>
                    <option value="mid" >활동적 또는 보통 활동(건설노동자 또는 매일 1시간씩 달림)</option>
                    <option value="high" selected>많은 활동(농업노동자 또는 매일 2시간동안 수영을 함)</option>
                    </select>
                   ` 
            }
            
            var html = template.html(title,`
            <h2>${title}</h2>
            <p>${description}</p>
            <form action="/countKcal_process" >
                성별 :<br />
                <select name="gender">
                    ${gender_default}
                </select>
                <br />
                키(cm) : <br /><input type="text" name="height" placeholder="${height}" /><br />
                몸무게(kg) : <br /><input type="text" name="weight" placeholder="${weight}"/><br />
                활동량 :<br />
                <select name="activity">
                   ${activity_default}
                <br />
                <input type="submit" value="계산하기" />
                <br>
                표준체중 : ${std_weight}kg <br>
                ${weightMsg}<br>
                BMI 지수 : ${BMI} / 당신은 ${BMI_msg} 입니다. <br>
                기초 대사량 : ${BMR}kcal <br>
                현재 체중 유지를 위한 열량 : ${TDEE}kcal <br>
            </form>
            ` );
            
           
            response.writeHead(200);
            response.end(html);
        });
    }
    else{
    response.writeHead(404);
    response.end("Not found");
    }
});
app.listen(23000);