2020-03-12

[nodejs] express 사용한 web server 개발 준비


개인적으로 nodejs 공부 하던중에 개인용으로 사용할 간단한 web server를 만들어보면서  블로그 업데이트를 해야겠다고 생각했다.
windows 10에 wsl ubuntu내에서 작업하였다.
nodejs를 공부하던 중이라서 1. 설치 정보의 nodejs, npm, express-generator는 이미 설치 되어 있었다.

1. 사전 설치 정보
$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 18.04.4 LTS
Release:        18.04
Codename:       bionic

$ nodejs --version
v12.16.1

$ npm --version
6.14.2

$ express --version
4.16.1

2. express 작업 디렉토리 설정
home_server라는 작업 디렉토리를 사용할것이다. 다른 디렉토리를 원하면 아래 명령에서 이름을 바꿔주면 된다.

$ express home_server --view=pug

   create : home_server/
   create : home_server/public/
   create : home_server/public/javascripts/
   create : home_server/public/images/
   create : home_server/public/stylesheets/
   create : home_server/public/stylesheets/style.css
   create : home_server/routes/
   create : home_server/routes/index.js
   create : home_server/routes/users.js
   create : home_server/views/
   create : home_server/views/error.pug
   create : home_server/views/index.pug
   create : home_server/views/layout.pug
   create : home_server/app.js
   create : home_server/package.json
   create : home_server/bin/
   create : home_server/bin/www

   change directory:
     $ cd home_server

   install dependencies:
     $ npm install

   run the app:
     $ DEBUG=home-server:* npm start

3. npm module 설치
express-generator를 사용하여 정상적으로 설정되면 마지막부분에 따라해야할 명령어들이 출력되어 있다. 그대로 실행해주면 된다.

$ cd home_server/
$ npm install
npm WARN deprecated core-js@2.6.11: core-js@<3 is no longer maintained and not recommended for usage due to the number of issues. Please, upgrade your dependencies to the actual version of core-js@3.

> core-js@2.6.11 postinstall /mnt/d/workspace/nodejs/home_server/node_modules/core-js
> node -e "try{require('./postinstall')}catch(e){}"

Thank you for using core-js ( https://github.com/zloirock/core-js ) for polyfilling JavaScript standard library!

The project needs your help! Please consider supporting of core-js on Open Collective or Patreon:
> https://opencollective.com/core-js
> https://www.patreon.com/zloirock

Also, the author of core-js ( https://github.com/zloirock ) is looking for a good job -)

npm notice created a lockfile as package-lock.json. You should commit this file.
added 118 packages from 174 contributors and audited 247 packages in 13.22s

2 packages are looking for funding
  run `npm fund` for details

found 1 low severity vulnerability
  run `npm audit fix` to fix them, or `npm audit` for details

4. 취약점 수정
3절 설치 과정 마지막에 취약점 정보가 1개 존재한다고한다.
친절하게 npm audit fix를 사용하라고 적혀있다.

$ npm audit fix
npm WARN deprecated core-js@2.6.11: core-js@<3 is no longer maintained and not recommended for usage due to the number of issues. Please, upgrade your dependencies to the actual version of core-js@3.
+ pug@2.0.4
removed 3 packages and updated 8 packages in 2.373s
fixed 1 of 1 vulnerability in 247 scanned packages

1개의 취약점 중 1개 수정되었다고 출력되었다. 혹시 모르니 다시 한번 입력해보았다.

$ npm audit fix
up to date in 0.532s

2 packages are looking for funding
  run `npm fund` for details

fixed 0 of 0 vulnerabilities in 244 scanned packages

5. test
npm start 명령으로 web server 실행후 웹브라우저로 접속하여보았다.

$ npm start

> home-server@0.0.0 start /nodejs/home_server
> node ./bin/www

5.1) 웹브라우저 접속 결과


5.2) 콘솔 출력 로그
GET / 304 387.352 ms - -
GET /stylesheets/style.css 200 4.508 ms - 111


express를 사용한 웹서버가 정상 동작하는것을 볼수 있다.