[Linux] web-was-db 연동

슬터디·2023년 11월 30일
0

[YOU] 기술분석

목록 보기
9/24

nginx Proxy 설정

  • nginx 설정 파일에서 명시한 다른 서버로 트래픽을 전달함
  • 설정 파일 위치: /etc/nginx/nginx.conf
  • 설정 후 서비스 재구동 하여야 설정 파일이 적용됨

  • vim /etc/nginx/nginx.conf
    • location / { proxy_pass http://[명시할 서버의 ip]:8000;}
  • systemctl restart nginx
  • systemctl status nginx

Web-Was 연동

  • nodejs 관련 작업은 /root/nodejs에다 진행함
  • nodejs 추가 모듈 설치(npm 사용)
    • npm install pg(postgreSQL 접속 위함)
    • npm install table(html 표 형태 출력을 위함)
    • npm install express(nodejs용 웹서버 패키지)
  • 이미지 로드를 위해 filezilla || scp를 활용해 파일을 옮겨주었다.

express를 활용한 웹서버 구성

  • vim 01-hello.js
    • 8000번 포트 listen 중
const express = require('express');
const app = express();
app.get('/', (req, res) => {
 res.send('Hello world with nginx & nodejs!');
});
app.get('/image', (req, res) => {
 res.sendFile(__dirname + '/logo.png');
});
app.listen(8000, () => {
 console.log('Server running on port 8000');
});
  • node 01-hello.js
    • 웹서버 구동
    • web, was ip 접속 시 모두 동일한 결과

WAS-DB 연동

  • vim 02-db.js
    • pg 모듈을 활용해 db의 comento db에 접속
    • select로 테이블 조회 후, table 형태로 저장해 회신
const express = require('express');
const { Client } = require('pg');
const Table = require('table');
const client = new Client({
 host: 'dbm01-IP',
 port: 5432,
 user: 'dba',
 password: 'dba',
 database: 'comento'
 });
client.connect();
const app = express();
app.get('/', (req, res) => {
 client.query('SELECT * FROM actor', (err, result) => {
 if (err) {
 console.error(err);
 res.send('Error executing query');
 return;
 }
 const data = result.rows.map(row => Object.values(row));
 const table = Table.table(data);
 res.send(table);
 });
});
app.listen(8000, () => {
 console.log('Server listening on port 8000');
});
  • node 02-db.js
profile
기억력이 맹구라 늘 기록해야해

0개의 댓글