본문으로 바로가기

node.js 설치 (centos7) + mysql 연결

category [ Web 관련 ]/node.js 2019. 8. 21. 17:56

Centos 버전 확인

# cat /etc/redhat-release

 

 

버전관리를 위한 NVM 설치 (선택) Node Version Manager

# curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.8/install.sh | bash

 

 

epel저장소가 없다면 저장소 추가

Extra Packages for Enterprise Linux : 엔터프라이즈 리눅스를 위한 추가 패키지

# yum repolist (조회)

# yum install epel-release

https://mainia.tistory.com/5614

 

저장소 설치 확인

# rpm -qa epel-release

 

 

nodejs, npm 설치 (설치 위치는 /lib/)

# yum install npm nodejs

 

 

버전 확인

# node -v

# npm -v

 

 

삭제

# yum remove -y nodejs

 

 

서버관리 모듈

<forever>

npm install  forever -g

forever start app.js

forever list

forever stop 0

 

<pm2> - 좀더 깔끔

npm install pm2 -g

pm2 start --name "앱 이름" xxx.js (앱이름은 필수 아니나 앱이 많을경우 구분하면 좋음)

pm2 list (목록확인)

pm2 stop id|name (정지)

pm2 restart id|name (재실행)

pm2 delete id|name (제거)

pm2 show id|name (상세정보 출력)

pm2 logs id|name (실시간 로그확인)

 

https://massivcode.com/5

 

부팅시 시작 되도록 설정하기

# pm2 startup

해제

pm2 unstartup

 

https://showerbugs.github.io/2018-04-03/Node-%EC%84%9C%EB%B2%84-%EC%84%B8%ED%8C%85-%EC%9D%B4%EC%95%BC%EA%B8%B0

 

 

 

 

node.js 업데이트

(react 사용시 현재 node.js v8이상 이어야 함)

 

https://nodejs.org/en/ 공식 홈페이지에서 버전 확인

 

  • 최신 버전 (Latest official release)
  • 안정 버전 (Stable official release)
  • LTS 버전 (Long-Term Support official release; 오랜 기간동안 안정적으로 사용할 수 있도록 지원하는 버전)



npm install 실행시에 Proxy 환경일 경우 제대로 설치가 안되는 경우가 있다.

 

이때에는 다음과 같이 npm config 명령을 통해 Proxy 설정이 필요하다.

 

예제

  • npm config set proxy http://111.111.111.111:8081
  • npm config set https-proxy https://111.111.111.111:8081
  • npm config set strict-ssl false
  • npm config set registry http://registry.npmjs.org/

npm 설정값은 다음 명령으로 확인가능하다.

  • npm config list

설정된 값은 ~/.npmrc 파일에 저장되어 있다.



출처: https://leechwin.tistory.com/entry/NPM-Proxy-설정 [Library of developer]     

 

 

 

현재 버전 확인

# node -v

 

Cache 삭제

캐시가 남아있는 경우 에러가 날 수 있다고 함.

# npm cache clean -f

 

 n 모듈 설치

# npm install -g n

 

최신 버전

# n latest

 

안정버전

# n stable

 

LTS버전

# n lts

 

특정버전 설치

# n 0.8.14
# n 0.8.17
# n 0.9.6

 

 

* proxy 사용할 경우 n모듈이 실행안된다

프록시 설정이 필요함

export http_proxy=http://192.168.12.237:3128
export https_proxy=http://192.168.12.237:3128

 

 

npm 업데이트

현재 버전 확인

# npm -v

 

npm 재설치

# sudo npm install -g npm

 

바로 반영이 안될경우 터미널을 재시작

 

참고 

https://futurecreator.github.io/2018/05/28/nodejs-npm-update-latest-or-stable-version/

 

 

 

 

 

 

mysql 연동 (API서버로 사용)

 

# mkdir test

# cd test

# npm init --yes

-> package.json 생성됨

 

express와 mysql 모듈을 설치한다.

# npm install -g express

# npm install -g mysql

# npm install -g body-parser 

 

 

proxy 사용할 경우.. npm사용시 설정 필요

# npm config set proxy http://localhost:3128

# npm config set https-proxy http://localhost:3128

 

해제

# npm config delete http-proxy

# npm config delete https-proxy

 

 

db정보를 저장할 db.json 생성

{
  "host" : "서버정보",
  "user" : "계정",
  "password" : "패스워드",
  "port" : "포트",
  "database" : "DB명"
}

 

app.js

const fs = require('fs');
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
const port = process.env.PORT || 5000;


app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended : true}));


const data = fs.readFileSync("./db.json");
const conf = JSON.parse(data);
const mysql = require('mysql');
const connection = mysql.createConnection({
    host     : conf.host,
    user     : conf.user,
    password : conf.password,
    port : conf.port,
    database : conf.database
});

connection.connect();

app.get('/api/test', function (req, res) {
    connection.query('SELECT * FROM blog_post',function ( err, rows, fields){
        if (err){
            console.log('executing query string is fail');
            throw err;
        } else {
            res.send(rows);
        }
    });

});


app.listen(port, function () {
    console.log(`Example app listening on port ${port}!`);
});






 

참조

https://poiemaweb.com/nodejs-mysql

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

<경고발생> 경고이므로 무시해도 됨

npm WARN nodejs@1.0.0 No description

-> package.json  에 "private": true 추가

{

"name": ...,

"description": ...,

"version": ...,

"private": true

}

 

npm WARN nodejs@1.0.0 No repository field.

-> git이 있다면 추가

"repository": {

"type": "git",

"url": "git://github.com/username/repository.git" 

}

 

 

 

npm install 실행시에 Proxy 환경일 경우 제대로 설치가 안되는 경우가 있다.

 

이때에는 다음과 같이 npm config 명령을 통해 Proxy 설정이 필요하다.

 

예제

  • npm config set proxy http://111.111.111.111:8081
  • npm config set https-proxy https://111.111.111.111:8081
  • npm config set strict-ssl false
  • npm config set registry http://registry.npmjs.org/

npm 설정값은 다음 명령으로 확인가능하다.

  • npm config list

설정된 값은 ~/.npmrc 파일에 저장되어 있다.



출처: https://leechwin.tistory.com/entry/NPM-Proxy-설정 [Library of developer]

 

[NPM] Proxy 설정

npm install 실행시에 Proxy 환경일 경우 제대로 설치가 안되는 경우가 있다. 이때에는 다음과 같이 npm config 명령을 통해 Proxy 설정이 필요하다. 예제 npm config set proxy http://111.111.111.111:8081 npm..

leechwin.tistory.com

 

 

 

 

 

참고

https://m.blog.naver.com/PostView.nhn?blogId=wlsdml1103&logNo=221160152394&proxyReferer=https%3A%2F%2Fwww.google.com%2F

 

https://yuddomack.tistory.com/entry/%EC%B2%98%EC%9D%8C%EB%B6%80%ED%84%B0-%EC%8B%9C%EC%9E%91%ED%95%98%EB%8A%94-EC2-nodejs-%EC%84%A4%EC%B9%98

 

https://javafa.gitbooks.io/nodejs_server_basic/content/chapter9.html