본문 바로가기

Programming/Node.js

How to integrate node.js with Asp.Net

Node.js

google에서 개발한 V8(java-script)을 기반으로 API를 제공하는 server platform 입니다.

특징

Java-script문법을 사용

Non blocking I/O


Prerequisites

Node.js

http://www.nodejs.org

 

IISNode

https://github.com/tjanczuk/iisnode

IIS 통합 Hosting 환경 구축

 

IIS Rewrite

http://www.iis.net/downloads/microsoft/url-rewrite

node linker로의 접근 지원

 

Web.config modify

System.webServer / handers를 추가

<add name="iisnode" path="hello.js" verb="*" modules="iisnode" />

 

node rewrite를 추가

<rewrite>
  <rules>
    <rule name="hello">
      <match url="hello/*" />
      <action type="Rewrite" url="hello.js" />
    </rule>
  </rules>
</rewrite>

 

Nodejs expand (ex, express) 경로 설정

<security>
  <requestFiltering>
    <hiddenSegments>
      <add segment="node_modules" />
    </hiddenSegments>
  </requestFiltering>
</security>

(Project root node_modules를 복사해야 함, 이는 node express의 설치 경로에서 확보 가능함)


적용 절차

1.     Node.js를 다운로드, 설치합니다.

A.     Download
http://nodejs.org/
로 접속하여, Install button으로 설치 파일 다운로드, 설치

B.      환경 변수 갱신
command
창에서 npm install formidable을 실행

2.     IISNODE를 다운로드, 설치합니다.

A.     Download
https://github.com/tjanczuk/iisnode/downloads
에서 PC 환경과 일치하는 설치 파일 다운로드 후 실행

3.     Web site를 생성합니다.

A.     IIS 관리 도구를 엽니다. (실행 창에서 inetmgr)

B.      응용 프로그램을 등록합니다.

4.     ASP.NET MVC4 solution을 생성합니다.

5.     Web.config파일을 수정하여, Node js를 사용할 수 있도록 합니다. (bold로 표기한 부분을 system.webServer/handlers에 추가)
<system.webServer>
  <
validation validateIntegratedModeConfiguration="false" />
  <
handlers>
    <
remove name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" />
    <
remove name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" />
    <
remove name="ExtensionlessUrlHandler-Integrated-4.0" />
    <
add name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" path="*." verb="GET,
    <
add name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" path="*." verb="GET,
    <
add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="GET,H
    <
add name="iisnode_1" path="test_query.js" verb="*" modules="iisnode" />
  </
handlers>
</
system.webServer>

6.  ASP.NET Controller
public JsonResult GetSamples()
{
   
return Json(new[] { new { Name = "bandcy", Age = 35, Gender = "Male", },
       
new { Name = "ethan", Age = 35, Gender = "Male", } }, JsonRequestBehavior.AllowGet);
}

7.  Node.js
var http = require('http');

function
onRequest(request, response) {
    http.get(
"http://localhost/NodejsTest/Home/GetSamples", function (result) {
       
//http.get("google.com", function (result) {
        result.on('data', function (chunk) {
            response.write(chunk);
        });
        result.on(
'end', function () {
            response.end();
        });
    });
}

http.createServer(onRequest).listen(process.env.PORT);


Reference

http://www.codeproject.com/Articles/683742/Node-Js-in-ASP-NET-MVC-4-in-IIS-8-Windows-8-1

http://www.imaso.co.kr/?doc=bbs/gnuboard.php&bo_table=article&wr_id=42584

http://blog.doortts.com/219

http://www.nodejskorea.org/

http://tomasz.janczuk.org/2011/08/hosting-nodejs-applications-in-iis-on.html

http://tomasz.janczuk.org/2011/08/hosting-express-nodejs-applications-in.html

http://www.hanselman.com/blog/InstallingAndRunningNodejsApplicationsWithinIISOnWindowsAreYouMad.aspx

http://alexjmackey.wordpress.com/2012/02/06/setting-up-node-on-windows/

http://geekswithblogs.net/shaunxu/archive/2012/09/14/node.js-adventure---node.js-on-windows.aspx

http://devroid.com/80196904236 (한글)

http://blog.naver.com/techshare?Redirect=Log&logNo=100202482947 (한글)