node.js
-
mongoDB 사용하기node.js 2020. 12. 2. 08:00
mongoDB를 웹페이지, 웹소켓 등에 두루 사용하기위해 mongoDB.js 모듈을 만든다. 필요에 맞는 스키마를 선택하여 불러와 사용할 수 있다. 서버(./routes/mongoDB.js) // DB 주소, 사용자정보 등은 노출되지않게 환경변수에 저장 require('dotenv').config(); const dbURL = process.env.dbURL; // mongoDB 컨트롤을 위한 mongoose 미들웨어 const mongoose = require('mongoose'); mongoose.set('useCreateIndex', true); mongoose.connect(dbURL, {useNewUrlParser:true,useUnifiedTopology:true}); mongoose.conn..
-
webSocket 연결node.js 2020. 12. 2. 02:34
동적 페이지 제작을 위해 웹소켓을 사용하여 서버와 클라이언트를 양방향으로 연결한다. 최신 브라우저는 대부분 지원하므로 socket.io같은 범용모듈을 사용하지 않고 그냥 html5 표준으로 제작(구형 익스플로러는 과감히 포ㅋ기ㅋ) 클라이언트 // 웹소켓 연결, http 서버와 같은 포트를 공유(웹소켓은 http 방식이라서 어쩌구...) const ws= new WebSocket('ws://localhost:8080'); // 기본 명령어 ws.onopen= (evt)=>{ alert(evt) }// 연결 ws.onclose= (evt)=>{ alert(evt) }// 연결 종료 ws.onerror= (evt)=>{ alert(evt) }// 에러 ws.onmessage= (evt)=>{ alert(evt..
-
crypto 미들웨어를 이용한 비밀번호 암호화node.js 2020. 11. 2. 23:41
const crypto = require('crypto'); var password = '예시'; crypto.randomBytes(64, (err, buf) => { var salt = buf.toString('base64'); crypto.pbkdf2( password, salt, 100000, 64, 'sha512', (err, key) => { return( key ).catch(err => { console.log(err) }) }) }) crypto 불러오기 crypto.randomBytes(64) : 64비트 길이의 문자열을 임의생성, 'buf'라는 인자로 반환 pbkdf2 : 단방향 암호화 방식 중 하나 pbkdf2() 에 들어가는 5개의 인자 암호화할 변수 salt : 해시된 암호는 쉽게..
-
passport 미들웨어를 이용한 로컬로그인node.js 2020. 11. 1. 23:46
온갖 종류의 로그인방식을 처리해주는 'passport' 모듈을 설치하여 로그인을 쉽게(?) 구현 ./roots/index.js require('dotenv').config(); const dbURL = process.env.dbURL; const mongoose = require('mongoose'); mongoose.set('useCreateIndex', true); mongoose.connect(dbURL,{ useNewUrlParser: true, useUnifiedTopology: true }); mongoose.connection.on('error', console.error.bind(console, "connection error:")); mongoose.connection.once('open..
-
cafe24 호스팅 설치 요약node.js 2020. 11. 1. 15:35
로컬 // 익스프레스 미들웨어로 서버설정 한번에 하기 npm install -g express express-generator // 프로젝트 디렉토리, pug 뷰엔진 생성 express [폴더이름] --view=pug // 해당 디렉토리로 이동 로컬 npm 모듈 설치 cd [폴더이름] && npm install // git 초기화, 설정 git init git remote add 등등... // pug 설치 npm install pug *cafe24 호스팅을 위한 파일 수정 ./bin/www 파일을 ./web.js로 위치와 이름을 바꿔준다. (cafe24가 ./bin/www 인식을 못하고 루트디렉토리의 web.js로만 인식) package.json 내용수정 "start": "node ./bin/www" ..