nodejs 14

HttpServer

// 서버 구축에 필요한 기능과 함수를 담고 있는 http모듈을 require 합니다 const http=require('http'); // createServer 함수 : Node.js 자바스크립트로 만든 http 서버가 실행되게 하는 함수입니다 // (req, res)=>{ } : 서버에 클라이언트가 요청이 있을때 실행할 명령들 또는 응답내용이 들어갑니다 // req를 이용하여 전달받은 요청을 res 를 이용하여 요청에 응답합니다. const server1 = http.createServer( (req, res)=>{ res.write('Hellow Server By Nose Js'); res.write( 'Wellcome to My Node Server!!' ); } ); // .listen( 80..

nodejs 2022.09.07

readAndWrite / promise

// 파일 읽기 쓰기를 위한 모듈 const fs = require('fs'); -fs.readFile fs.readFile( './readMe.txt' , (err, data)=>{ if( err ){ // err 변수에 무언가 들어있다면, 파일을 읽어오다가 에러가 발생했다면 throw err; } console.log(data); console.log(data.toString()); }); // err : 파일읽기에 실패했을때 전달되는 값을 받는 매개 변수 // data : 파일읽기에 성공했을때 읽어온 파일 내용 데이터 -> data.toString()은 readMe.txt에 있는 글자를 읽어옵니다. -readFilePromises // 파일 입출력을 위한 모듈의 promise 포함하여 로딩합니다 c..

nodejs 2022.09.06

Path

const path = require('path'); // path 가 아니어도 사용 가능한 경로와 파일관련 상수 console.log(__filename); // 현재사용중인 파일의 이름 console.log(__dirname); // 현재 파일이 위치한 경로 // 현재 경로와 파일의 이름을 변수에 저장하여 별도 출력 const string = __filename; console.log( string ); // 파일이 위치한 폴더 경로 를 보여줍니다 console.log('path.dirname():', path.dirname(string)); // 파일의 확장자(.js)를 보여줍니다 console.log('path.extname():', path.extname(string)); // 파일의 이름+확장..

nodejs 2022.09.06

Os

// 자바에서 임포트해서 쓰던 클래스 -> 자바스크립트에서는 Module 을 require 한다고 표현하고 아래와 같이 기술합니다 // os 모듈의 기능을 담고 있는 객체를 불러와서 os 변수(const 형 변수) 에 저장하고 사용하겠습니다라는 뜻 const os = require('os'); console.log('운영체제 정보------------------------------------------------'); console.log('os.arch() : ', os.arch()); // 현재 운영체제의 설계 및 운영방식 console.log('os.platform() : ', os.platform()); // 운영체제 기반 플랫폼 console.log('os.type():', os.type())..

nodejs 2022.09.06

Timer

// 지정된 시간 후에 한번 실행 const timeout = setTimeout( ()=>{ console.log('1.5초 후 실행');} , 1500); // 지정된 시간마다 반복 실행 const interval = setInterval( ()=>{console.log('1초마다 실행');} , 1000); // 즉시실행 const immediate = setImmediate( ()=>{ console.log('즉시 실행'); } ); // 타이머 종료 clearTimeout(timeout); // 아직 지정된 시간이 지나지 않았다면 실행전 종료 clearInterval(interval); // 반복실행 종료 clearImmediate(immediate); //즉시실행 종료

nodejs 2022.09.06

Console

const string = 'abc'; const number = 1; const boolean = true; const obj = { outside: { inside: { key: 'value', }, }, }; console.log('평범한 로그입니다 쉼표로 구분해 여러 값을 찍을 수 있습니다'); console.log(string, number, boolean); console.log(); // 같은 텍스트이여도 에러 메세지는 console.error() 에 담아 출력합니다 console.error('에러 메시지는 console.error에 담아주세요'); console.log(); // console.table() 안의 객체 모양의 데이터들을 테이블 형태로 출력합니다. console.table( ..

nodejs 2022.09.06

anotherPromise

//Promise 결과를 별도의 함수 안에서 활용할 때 많이 사용하는 방법입니다. const condition = true; const promise1 = new Promise((resolve, reject) =>{ if(condition) resolve('성공'); else reject('실패'); }); async function abcd(){ try{ const result = await promise1; console.log('1'+result); }catch(error){ console.error('2' + error); } } abcd(); //promist1 객체를 result 변수에 저장합니다. // await : promise 의 비동기실행의 결과를 필요할때 꺼내쓰기 위한 키워드 //함수..

nodejs 2022.09.06

Promise

Promise // 함수와 비슷한 기능을 갖고 있는 객체. // 객체내의 익명함수의 내용을 실행하고 , 결과를 보관하고 있다가, 결과가 필요할때 전달 받아 사용할수 있게 해주는 구조의 객체 // const pm = new Promise( /* 익명함수 */ ); // promise 객체의 전달인수 없는 선언문. // 익명함수 한개를 품고 있는 객체 생성 & 객체변수에 저장 // promise 객체는 생성자의 전달인수로 익명함수를 전달하여야 생성되는데, 이 익명함수는 Promise 의 기능이기도 합니다 // func = (resolve, reject) => { } // const pm = new Promise( func ); 또는 const pm = new Promise( (resolve, reject) ..

nodejs 2022.09.06

ArrowFunction

// function 함수이름(매개변수){ } -> 화살표를 이용한 표현방법으로 사용 // 함수의 표현방법 #1 function add1(x, y){ return x+y; } console.log( add1(10, 20) ); ->30 출력 // 함수의 표현방법 #2 let add2 = function( x, y){ return x+y; } console.log( add2(10, 20) ); ->30출력 // 함수의 표현 방법 #3-1 ( 화살표함수 ) const add3=(x, y)=>{ return x+y; } // 익명함수 (x, y)=>{ return x+y; } 가 add3 에 저장 console.log( add3(10, 20) ); ->30출력 // 함수의 표현 방법 #3-2 const add..

nodejs 2022.09.06