본문 바로가기

Nodejs

(7)
[Node.js] 프로젝트 생성 및 mongoDB 연결하기 프로젝트 생성 및 설정 1. 프로젝트 시작하기 (내 프로젝트 package화) npm init 더보기 ( * 괄호 안에 값이 있으면 엔터를 쳤을 때 자동으로 설정됩니다. * ) name : 프로젝트 이름 version : 버전 description : 설명 (직접 쓰기를 권장) entry point: 어떤 js파일이 package를 구동시키는 첫 시작인지 test command : TDD(Test Driven Development : 테스트 주도 개발)를 할 때 test를 실행시킬 명령어 git repository : git repo 주소 1-1. 이미 client가 있다면 server 폴더를 만들고 cd server 2. express와 nodemon, mongoose 설치 npm install exp..
[Node.js] supervisor 서버 자동on/off supervisor 코드를 수정하면 자동으로 node.js를 껐다 키기 때문에 application을 수동으로 껐다 켤 필요가 없다. 메뉴얼 : https://www.npmjs.com/package/supervisor 설치 npm install supervisor -g supervisor app.js
[Node.js] 서버에 데이터 저장 (파일로 저장) 파일로 데이터 저장하기 (데이터 입력) //app.js const fs = require('fs'); app.set('views'. './views_file'); app.set('view engine', 'jade'); app.post('/topic', function(req,res){ const title = req.body.title; const desc = req.body.desc; fs. writeFile('data/'+title, desc, function(err){ if(err){ console.log(err); res.status(500).send('Internal Server Error'); // status(500) : 오류 } res.send('Success!'); }); } }) //..
[Node.js] url 해부하기(query, params), Form으로 데이터 전달 url 해부하기 ex) http://a.com/topic?id=1 http : 프로토콜 a.com : 도메인. 서버 컴퓨터가 위치하는 주소 topic : path. directory명 또는 router와 연결되는 주소 id = 1 : query string url req이 들어오면 해당하는 router에 걸려 res를 해준다. url에 해당하는 router가 없다면 404 에러가 뜬다. query 객체사용법 query를 통해 사용자가 query string으로 요청한 정보를 사용할 수 있다. //http://a.com/topic?name=Baeji => 출력 : Baeji app.get('/topic', function(req, res){ res.send(req.query.name); }) //http:..
[Node.js] express와 jade 설치 및 사용법 express 설치 디렉토리 생성 및 이동 mkdir myapp cd myapp 프로젝트를 npm이 관리하는 패키지로 선언 (=> package.json 생성) npm init npm이 express를 다운 npm install --save express 사용법 main(entry):최초의 진입점 application 만들기 (app.js) var express = require("express"); var app = express(); app.use(express.static("")); app.get("/route", function (req, res) { res.send('hello, '); }); app.get("/", function (req, res) { res.send("hello home p..
[Node.js] 동기, 비동기 https://nodejs.org/docs/latest-v15.x/api/fs.html 동기, 비동기 * 동기 ex) fs.readFileSync (파일읽기) const fs = require("fs"); const data = fs.readFileSync("파일명", { encoding: "utf8" }); console.log(data); 더보기 { encoding : "utf8" }은 파일(txt)을 저장할 때 utf8 방식으로 저장해서 읽어올 때도 이 방식으로 읽어와야 함. * 비동기 ex) fs.readFile (파일 읽기) const fs = require("fs"); fs.readFile("파일명", { encoding: "utf8" }, function (err, data) { if (er..
[Node.js] 인터넷, 모듈, NPM 프로젝트 시작, Callback OT js는 language(java script), run time(web browser과 node js)라고 할 수 있습니다. 즉, js는 언어일뿐이고 js가 web brower와 node js에서 어떤 기능이 있는지 알아야 합니다. 예를 들어 alert는 web browser에서만 사용되고 node js에서는 사용할 수 없다는 것이 있습니다 . Node js 의 장점 기본적으로 v8을 사용하기 때문에 속도가 빠릅니다. event driven과 non-blocking을 사용해 이 특징이 적합할 땐 아주 빨라집니다. js라는 하나의 언어로 client와 server 모두 구현 가능합니다. 인터넷의 동작 방법 1. client와 server 컴퓨터끼리 연결될 때 요청을 보내는 쪽이 client, 요청을 ..