HyunJun 기술 블로그

Node.js & Express 설치 본문

Node.js

Node.js & Express 설치

공부 좋아 2023. 6. 15. 17:18
728x90
반응형

Node.js 설치

1) GUI 환경.

https://nodejs.org/ko -> LTS Download -> 설치 (node.js 설치 시 npm 자동 설치됨.)

 

2) CLI 환경.

nvm (Node Version Manager) 설치

$ curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
$ source $HOME/.zshrc nvm ls-remote node nvm install v16.15.1

$ nvm --version
0.39.1

 

node.js 설치

$ nvm install --lts
Installing latest LTS version.
Downloading and installing node v18.17.1...
Downloading https://nodejs.org/dist/v18.17.1/node-v18.17.1-linux-x64.tar.xz...
######################################################################### 100.0%
Computing checksum with sha256sum
Checksums matched!
Now using node v18.17.1 (npm v9.6.7)
Creating default alias: default -> lts/* (-> v18.17.1)
$ node --version
v18.17.1

 

Node.js 프로젝트 생성

1) app.js 생성

VS Code Project 생성 -> 터미널 - > npm init 입력 후, 

  • package name:
  • version:
  • description:
  • entry point: app.js
  • test command:
  • git repository:
  • keywords: node test
  • author: 제작자
  • license: (ISC)
  • Is this OK? (yes) yes
hyunjun@MacBook-Pro kyungil-node % npm init
This utility will walk you through creating a package.json file.
It only covers the most common items, and tries to guess sensible defaults.

See `npm help init` for definitive documentation on these fields
and exactly what they do.

Use `npm install <pkg>` afterwards to install a package and
save it as a dependency in the package.json file.

Press ^C at any time to quit.
package name: (kyungil-node) com.test
version: (1.0.0) 
description: nodejs install
entry point: (index.js) app.js
test command: 
git repository: 
keywords: node test
author: hyunjun
license: (ISC) 
About to write to /Users/hyunjun/Library/Mobile Documents/com~apple~CloudDocs/IT/kyungil/kyungil-node/package.json:

{
  "name": "com.test",
  "version": "1.0.0",
  "description": "nodejs install",
  "main": "app.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [
    "node",
    "test"
  ],
  "author": "hyunjun",
  "license": "ISC"
}


Is this OK? (yes) yes

위의 npm init 과정을 거치면, 패키지와 프로젝트의 정보를 기록하고 관리하는 파일인 package.json 파일이 생성된다.

 

 

2) express 설치

hyunjun@MacBook-Pro kyungil-node % npm install --save express

added 58 packages, and audited 59 packages in 674ms

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

found 0 vulnerabilities
hyunjun@MacBook-Pro kyungil-node %

package-lock.json 파일이 생기며, node_modules라는 폴더가 생긴다.

 

 

3) app.js 기본 코드

app.js라는 파일을 생성한다.

기본 Controller 코드 작성

const express = require("express");
const app = express();

app.get("/", function (req, res) {
  res.send("Hello World");
});

app.listen(3000, function () {
  console.log("3000 port listen");
});

 

 

4) 앱 실행(서버 실행)

hyunjun@MacBook-Pro kyungil-node % node app.js
3000 port listen !!

 

5) 테스트

브라우저로 localhost:3000 접속.

728x90
반응형
Comments