더북(TheBook)

5.5.2 회원 관리 기능 만들기

회원 관리에 필요한 기능을 추가하겠습니다. monolithic_members.js 파일에서 기존 코드에 다음 register 함수를 추가하고, 앞과 동일한 패턴으로 코드도 추가합니다.

코드 5-16 회원 등록

예제 파일 : monolithic_members.js

const mysql = require(‘mysql’);
const conn = {
    host: ‘localhost’,
    user: ‘micro’,
    password: ‘service’,
    database: ‘monolithic’
};

……

function register(method, pathname, params, cb) { var response = { key: params.key, errorcode: 0, errormessage: “success” };

<span class="k">if</span> <span class="p">(</span><span class="nx">params</span><span class="p">.</span><span class="nx">username</span> <span class="o">==</span> <span class="kc">null</span> <span class="o">||</span> <span class="nx">params</span><span class="p">.</span><span class="nx">password</span> <span class="o">==</span> <span class="kc">null</span><span class="p">)</span> <span class="p">{</span>
    <span class="nx">response</span><span class="p">.</span><span class="nx">errorcode</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span>
    <span class="nx">response</span><span class="p">.</span><span class="nx">errormessage</span> <span class="o">=</span> <span class="s2">"Invalid Parameters"</span><span class="p">;</span>
    <span class="nx">cb</span><span class="p">(</span><span class="nx">response</span><span class="p">);</span>
<span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
    <span class="kd">var</span> <span class="nx">connection</span> <span class="o">=</span> <span class="nx">mysql</span><span class="p">.</span><span class="nx">createConnection</span><span class="p">(</span><span class="nx">conn</span><span class="p">);</span>
    <span class="nx">connection</span><span class="p">.</span><span class="nx">connect</span><span class="p">();</span>
    <span class="nx">connection</span><span class="p">.</span><span class="nx">query</span><span class="p">(</span><span class="s2">"insert into members(username, password)</span>
                    <span class="s2"> values('"</span> <span class="o">+</span> <span class="nx">params</span><span class="p">.</span><span class="nx">username</span> <span class="o">+</span> <span class="s2">"', password('"</span> <span class="o">+</span>
                     <span class="nx">params</span><span class="p">.</span><span class="nx">password</span> <span class="o">+</span> <span class="s2">"'));"</span><span class="p">,</span> <span class="p">(</span><span class="nx">error</span><span class="p">,</span> <span class="nx">results</span><span class="p">,</span> <span class="nx">fields</span><span class="p">)</span> <span class="p">=&gt;</span> <span class="p">{</span>
        <span class="k">if</span> <span class="p">(</span><span class="nx">error</span><span class="p">)</span> <span class="p">{</span>
            <span class="nx">response</span><span class="p">.</span><span class="nx">errorcode</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span>
            <span class="nx">response</span><span class="p">.</span><span class="nx">errormessage</span> <span class="o">=</span> <span class="nx">error</span><span class="p">;</span>
        <span class="p">}</span>
        <span class="nx">cb</span><span class="p">(</span><span class="nx">response</span><span class="p">);</span>
    <span class="p">});</span>
    <span class="nx">connection</span><span class="p">.</span><span class="nx">end</span><span class="p">();</span>
<span class="p">}</span>

}

신간 소식 구독하기
뉴스레터에 가입하시고 이메일로 신간 소식을 받아 보세요.