[mongoDB] DB 맛보기 - 공부기록

hyunwoo Jin·2022년 6월 8일
0
// 파일(index.html) 열어준다.
var fs = require('fs')
// express
var express = require('express');
// 변수명을 app 사용하더라
var app = express();
// mongoDB 관련
var MongoClient = require('mongodb').MongoClient;
var mongoUrl = 'mongodb://localhost:27017';
var mongo;

function connMongo(){
    // DB 접속해서 데이터베이스를 연결한다.
    MongoClient.connect(mongoUrl, function(err, db){
        if(err) throw err;
        console.log(mongoUrl + "연결되었다");
        mongo = db.db('firstdb');
    });
}

// fourthcol 컬렉션의 전체 내용 출력
app.get('/col', function(request, response){

    MongoClient.connect(mongoUrl, function(err, db){
        if(err) throw err;
        mongo.collection('fourthcol').find({}).toArray(function(err, result){
            if(err) throw err;
            response.send(result);
            return;
        });
    })
});

// fourthcol 컬렉션의 내용 1개 추가
//localhost/insert?name=Park&height=180&depart=Front
app.get('/insert', function(request, response){
    console.log(request);
    MongoClient.connect(mongoUrl, function(err, db){
        if(err) throw err;
        mongo.collection('fourthcol').insertOne(
            request.query, function(err, result){
                if(err) throw err;
                response.send(result);
                return;
            }
        );
    })
});

// app 에서 get 요청을 한다.
app.get('/', function(request, response){
    fs.readFile('index.html', function(err, data){
        response.writeHead(200, {'Content-Type': 'text/html'});
        response.end(data);
        console.log('Home');
    });
});

app.listen(80, function(){
    console.log('사용중인 포트는 80번입니다');
    connMongo();
});

profile
꾸준함과 전문성

0개의 댓글