arch.ruaniz.com

Scott.Lee, Application Architect

Install and setup : node.js

Created 목요일 30 11월 2017
Node.js는 구글에서 개발한 고성능 자바스크립트 엔진 V8로 빌드된 서버 사이드 개발용 소프트웨어 플랫폼이다.

1. Pre-installation setup

A. creating required operating system group and user

Create operating system group and user

B. install dependency packages

C. creating base directory

D. firewall configuration

2. installation setup : app

A. NVM(Node Version Manager) : https://github.com/creationix/nvm

Install script

설치가 되면 ~/.bash_profile, ~/.zshrc, ~/.profile 등의 프로파일에 nvm.sh이 실행되도록 다음 스크립트가 추가된다.

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm

Verify installation
$ command -v nvm

B. Node.js : https://nodejs.org/en/

LTS 최신 버전 설치
$ nvm install –lts

Node 버전 설치
$ nvm install # ex> nvm install 10.16.3

설치된 Node 버전 목록 확인
$ nvm ls

사용할 Node 설정
$ nvm use # ex> nvm use 8.9.4 $ nvm use # ex> nvm use default

default alias version
$ cat ~/.nvm/alias/default

사용할 alias 설정
$ nvm alias # ex> nvm alias test-v 8.9.4

Node 삭제
$ nvm uninstall # ex> nvm uninstall 8.9.4

Verify installation
$ command -v nvm

2. Development Environment (eclipse)

Eclipse Install : http://www.eclipse.org/downloads/

Package Install
Eclipse IDE for Java EE Developers (recommended)

or Eclipse IDE for JavaScript and Web Developers

Create Project (JavaScript)

  1. Create Project
    File > New > Project > JavaScript > JavaScript Project
    Project Name : prj.surface

  2. Encoding setup
    Window > Preferences > General > Workspace > Text file encoding : UTF-8

  3. NPM Initialize
    File > New > Other… > JavaScript > npm Init

  4. Node.js Module Install
    1. open terminal
      • add/create eclipse external script : run a program
        Run > External Tools > External Tools Configurations

        Project Name : terminalOpen
        Location : /usr/bin/xfce4-terminal
        Working Directory : ${resource_loc}

    2. Express Module
      • Express : 웹 요청을 처리
      • Realm : 데이터베이스 활용
      • EJS(Embedded JavaScript) : 템플릿 처리
      • body-parser : 폼으로 전달된 쿼리를 처리

      • Morgan : 메시지 콘솔 표시
      • Compression : 페이지 압축 전송
      • Session : 세션 처리
      • Cookie-parser : 쿠키 사용
      • Method-override : REST API에서 PUT과 DELETE 메소드를 사용
      • Cors : 크로스오리진(다른 도메인 간의 AJAX 요청) 가능
      • Multer : 파일업로드

      $ npm install –save express
      $ npm install –save realm
      $ npm install –save ejs
      $ npm install –save body-parser

      “–save” option : 설치된 모듈을 package.json에 기록

    3. eclipse project refresh
  5. Dependent Packages/Modules Install with package.json
    $ npm install
  6. Create a new JavaScript file
    File > New > JavaScript Source File

    File Name : index.js

Environment Extensions (optional)

  1. node.js plugin : Express
    Global Install
    $ npm install -g express
    $ npm install -gd express-generator

  2. Eclipse plugin

    1. Nodeclipse Plugin Install
      Help > Eclipse Marketplace… > “node” Search
      Nodeclipse Install

    2. Nodeclipse Plugin Setup
      Window > Preferences > Nodeclipse
      Node Path: ~/.nvm/versions/node/v8.11.4/bin/node
      Express Path: ~/.nvm/versions/node/v8.11.4/bin/express

Project Extensions (optional)

  1. Create Node.js Express Project
    File > New > Node Project
    Project Name : mobon.ad.surface
    Template Engine : select ejs
    Stylesheet Engine : select stylus

  2. Execute
    Project Explorer > mobon.ad.surface > (right click) Run As > Node.js Application
    Main file : ${workspace_loc:/mobon.ad.surface/bin/www}
    Apply > Run
    Error: Cannot find module ‘http-errors’ …

  3. Dependent Packages/Modules Install with package.json
    Project Explorer > mobon.ad.surface > (right click) StartExplorer > Start Shell Here
    npm install

8. Troubleshooting

ISSUE

/bin/bash: 줄 1: npm: 명령어를 찾을 수 없음.

cause
eclipse가 non-login shell로 실행되면서 /etc/profile과 /etc/bashrc만 실행되고 login(interactive) shell에서 사용하는 ~/.bashrc, ~/.profile, ~/.bash_profile이 실행되지 않는다.

solution
non-login shell에서 사용하는 /etc/profile과 /etc/bashrc 스크립트에 Path 추가
eclipse를 login shell로 기동 : ex) bash -ic “eclipse”
(nvm 이하 node.js가 ~/.nvm에 설치되므로 2번으로 해결)

9. Appendix

reference site

Express 코어 및 미들웨어 시스템에 대한 변경

Express 4는 더 이상 Connect에 종속되지 않으며, express.static 함수를 제외한 모든 기본 제공 미들웨어가 Express 4 코어에서 제거되었다.
따라서 Express는 독립적인 라우팅 및 미들웨어 웹 프레임워크가 되었으며, Express 버전화 및 릴리스는 미들웨어 업데이트의 영향을 받지 않게 되었다. 기본 제공 미들웨어가 없어 사용자는 앱 실행에 필요한 모든 미들웨어를 명시적으로 추가해야 한다.

  1. 모듈 설치: npm install –save
  2. 내에서, 모듈 요청: require(‘module-name’)
  3. 해당 모듈의 문서에 따라 모듈 사용: app.use( … )

xpress 3의 미들웨어 및 그에 대응하는 Express 4의 미들웨어

Express 3 Express 4
express.bodyParser body-parser + multer
express.compress compression
express.cookieSession cookie-session
express.cookieParser cookie-parser
express.logger morgan
express.session express-session
express.favicon serve-favicon
express.responseTime response-time
express.errorHandler errorhandler
express.methodOverride method-override
express.timeout connect-timeout
express.vhost vhost
express.csrf csurf
express.directory serve-index
express.static serve-static

Express 4 미들웨어의 전체 목록 참조

PM2 - Node.js 프로세스 관리 도구

pm2 명령어를 사용해야 하므로 npm을 이용해서 전역으로 설치한다.
$ npm install pm2 -g
$ pm2 version

$ pm2 start app.js
$ pm2 list
$ pm2 show app
$ pm2 delete app

reference site