profile image

L o a d i n g . . .

새로 진행하게 될 프로젝트는 서버사이드 까지 관리할 것이다!

 

Node >>

 개발자가 모든 종류의 서버사이드 도구들과 app을 자바스크립트로 만들수 있도록 해주는 런타임 환경

패키지를 만들기위해 터미널을 사용해주었다.

 

 

 

 

 

 

이제 Express.js 설치

 

Express/Node 소개 - Web 개발 학습하기 | MDN

첫번째 Express 수업에서는 Node, Express를 알아보고, Express 웹 프레임워크 제작의 전반에 대해 배우게 됩니다. 우선 주요 특징들에 대한 틀을 정리한 후 Express 어플리케이션을 구성하는 주요 구성요

developer.mozilla.org

 

 

 

$ npm install express --save

 

Express - Express.js, 또는 간단히 익스프레스는 Node.js를 위한 웹 프레임워크의 하나로, MIT 허가서로 라이선스되는 자유-오픈 소스 소프트웨어로 출시되었다. 웹 애플리케이션, API 개발을 위해 설계되었다. Node.js의 사실상의 표준 서버 프레임워크로 불리고 있다. 

 

 

Express - Node.js 웹 애플리케이션 프레임워크

Node.js를 위한 빠르고 개방적인 간결한 웹 프레임워크 $ npm install express --save

expressjs.com

아까 패키지.json에는 없던 express 내용이 생겼다

 

 

 

 

 

const express = require('express');
const app = express();
const port = 5000;

app.get('/', (req, res) => res.send('hello world'));

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

index.js에 코드를 입력하고

 

package.json scripts부분에 start에 해당하는 실행문을 적어준다

    "start": "node index.js"

 

그리고 터미널에 npm run start를 적어주면 

이제 app이 5000 port에 접근하면 내용을 프린트해주게된다 

 

 

 

 

 

 

 

몽고DB

몽고DB(MongoDB←HUMONGOUS)는 크로스 플랫폼 도큐먼트 지향 데이터베이스 시스템이다. NoSQL 데이터베이스로 분류되는 몽고DB는 JSON과 같은 동적 스키마형 도큐먼트들(몽고DB는 이러한 포맷을 BSON이라 부름)을 선호함에 따라 전통적인 테이블 기반 관계형 데이터베이스 구조의 사용을 삼간다. 이로써 특정한 종류의 애플리케이션을 더 쉽고 더 빠르게 데이터 통합을 가능케 한다. 

- Json타입의 NOSQL로 불필요한 Join을 줄이고 빠른 속도와 저렴한 비용이 장점이다

 

The most popular database for modern apps

We're the creators of MongoDB, the most popular database for modern apps, and MongoDB Atlas, the global cloud database on AWS, Azure, and GCP. Easily organize, use, and enrich data — in real time, anywhere.

www.mongodb.com

 

 

몽고디비 사이트 회원가입하고 위 옵션에 맞추어 클러스터 생성을 해주면 된다.

 

생성 이후에 등록하는 아이디와 비밀번호는 잊지않도록 어디다 메모해두기.

 

 

 

 

 

클러스터가 생성되면 우측에 연결하기를 누른 후 애플리케이션 연결을 해준다.

그리고 뜨는 애플리케이션코드또한 잘 저장해두자

 

 

 

 

 

 

Mongoose

몽고DB를 편하게 쓸 수 있는 Object Modeling Tool

npm install mongoose --save

 

몽구스를 이용해서 app 이랑 MongoDB를 연결할 것이다.

const mongoose = require('mongoose');
mongoose.connect('mongodb + srv://inadang:<password>@inadang.layhnvt.mongodb.net/?retryWrites=true&w=majority', {
    useNewUrlParser: true, useUnifiedTopology: true, useCreateIndex: true, useFindAndModify: false
});

 

 

 

MongoDB Model과 스키마 생성

Model : 스키마를 감싸주는 역할

 

웹을 이용할 때 가장 기본인 회원가입을 위한 User 데이터 보관을 위한 User Model을 만들었다.

const mongoose = require('mongoose');

const userSchema = mongoose.Schema({
    name: {
        type: String,
        maxlength: 50
    },
    email: {
        type: String,
        trim: true,
        unique: 1
    },
    password: {
        type: String,
        minlength: 5
    },
    lastname: {
        type: String,
        maxlength: 50
    },
    role: {
        type: Number,
        default: 0
    },
    image: String,
    token: {
        type: String
    },
    tokenExp: {
        type: Number
    }
})

const User = mongoose.model('User', userSchema);

module.exports = { User };

 

 

 

 

참고 :

- 위키백과 Express.js, MongoDB

 

 

 

 

반응형
복사했습니다!