더북(TheBook)

5.4.2 회원 관리 모듈 만들기

monolithic_members.js 파일을 생성하고 다음과 같이 코드를 작성합니다.

코드 5-9 회원 관리 모듈 만들기

예제 파일 : monolithic_members.js

exports.onRequest = function(res, method, pathname, params, cb) {

<span class="k">switch</span> <span class="p">(</span><span class="nx">method</span><span class="p">)</span> <span class="p">{</span>
    <span class="k">case</span> <span class="s2">"POST"</span><span class="o">:</span>
        <span class="k">return</span> <span class="nx">register</span><span class="p">(</span><span class="nx">method</span><span class="p">,</span> <span class="nx">pathname</span><span class="p">,</span> <span class="nx">params</span><span class="p">,</span> <span class="p">(</span><span class="nx">response</span><span class="p">)</span> <span class="p">=&gt;</span> <span class="p">{</span>
                        <span class="nx">process</span><span class="p">.</span><span class="nx">nextTick</span><span class="p">(</span><span class="nx">cb</span><span class="p">,</span> <span class="nx">res</span><span class="p">,</span> <span class="nx">response</span><span class="p">);</span> <span class="p">});</span>
    <span class="k">case</span> <span class="s2">"GET"</span><span class="o">:</span>
        <span class="k">return</span> <span class="nx">inquiry</span><span class="p">(</span><span class="nx">method</span><span class="p">,</span> <span class="nx">pathname</span><span class="p">,</span> <span class="nx">params</span><span class="p">,</span> <span class="p">(</span><span class="nx">response</span><span class="p">)</span> <span class="p">=&gt;</span> <span class="p">{</span>
                       <span class="nx">process</span><span class="p">.</span><span class="nx">nextTick</span><span class="p">(</span><span class="nx">cb</span><span class="p">,</span> <span class="nx">res</span><span class="p">,</span> <span class="nx">response</span><span class="p">);</span> <span class="p">});</span>
    <span class="k">case</span> <span class="s2">"DELETE"</span><span class="o">:</span>
        <span class="k">return</span> <span class="nx">unregister</span><span class="p">(</span><span class="nx">method</span><span class="p">,</span> <span class="nx">pathname</span><span class="p">,</span> <span class="nx">params</span><span class="p">,</span> <span class="p">(</span><span class="nx">response</span><span class="p">)</span> <span class="p">=&gt;</span> <span class="p">{</span>
                          <span class="nx">process</span><span class="p">.</span><span class="nx">nextTick</span><span class="p">(</span><span class="nx">cb</span><span class="p">,</span> <span class="nx">res</span><span class="p">,</span> <span class="nx">response</span><span class="p">);</span> <span class="p">});</span>
    <span class="k">default</span><span class="o">:</span>
        <span class="k">return</span> <span class="nx">process</span><span class="p">.</span><span class="nx">nextTick</span><span class="p">(</span><span class="nx">cb</span><span class="p">,</span> <span class="nx">res</span><span class="p">,</span> <span class="kc">null</span><span class="p">);</span>
<span class="p">}</span>

}

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