본문 바로가기

web/nodejs

Express 프로젝트 Boilerplate 1. 모듈 가져오기 const express = require("express"); const bodyParser = require("body-parser"); const ejs = require("ejs"); const mongoose = require('mongoose'); Express - 웹서버 빌딩을 위한 모듈 body-parser - Http response 메시지를 파싱하기 위한 미들웨어 ejs - html 템플릿 엔진 mongoose - nodejs에서 mongoDB를 쉽게 사용하기 위한 모듈 2. 가져온 모듈 세팅 const app = express(); Express를 사용하기 위해 객체 저장 app.set('view engine', 'ejs'); HTML 템플릿 엔진을 ejs로 사용하.. 더보기
Express를 이용한 MongoDB RESTful API 서버 만들기 준비 사항 - MongoDB 설치, 서버 가동 - MongoDB GUI Tool - Robo 3T 설치 Http 메소드와 Endpoint에 따른 요청을 정리하면 다음과 같다. HTTP Methods /articles /articles/sports GET Fetches all the articles Fetches the article on sports POST Creates one new article - PUT - Updates the article on sports PATCH - Updates the article on sports DELETE Deletes all the articles Deletes the article on sports 1. 기본 프로젝트 세팅 const express = requ.. 더보기
Mongoose 사용법 -Node에서 mongoDB 연결 + CRUD - MongoDB 구조 : Database > Collection > Document Node에서 MongoDB를 사용하는 방법은 여러가지가 있다. MongoDB에서 자체적으로 제공하는 Driver를 사용해도 되지만 Mongoose를 사용하는 것이 훨씬 간편하다. Moogoose API Docs를 참고하여 작성하였다. 1. mongoose 모듈 가져오기, Node앱을 MongoDB에 연결하기 const mongoose = require("mongoose"); // mongodb://[URL of MongoDB server]/[Name of DB to connect(make one if doesn't exist)] mongoose.connect("mongodb://localhost:27017/fruitsD.. 더보기
.gitignore for Node # Logs logs *.log npm-debug.log* yarn-debug.log* yarn-error.log* lerna-debug.log* # Diagnostic reports (https://nodejs.org/api/report.html) report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json # Runtime data pids *.pid *.seed *.pid.lock # Directory for instrumented libs generated by jscoverage/JSCover lib-cov # Coverage directory used by tools like istanbul coverage *.lcov # nyc test coverage .nyc_output # G.. 더보기