728x90
반응형
SMALL
/**
 * REST(Representational State Transfer)
 * 자원을 이름으로 구분하여 해당 자원의 상태를 주고받는 모든 것을 의미
 *
 * API (application programming interface)
 * 기능의 집합을 제공해서 컴퓨터 프로그램간 상호작용을 하도록 하는 것
 *
 * Rest Api
 * Rest 기반으로 서비스 API를 구현한 것
 *
 * CRUD Operation
 * POST: 생성(create)
 * GET: 조회(read)
 * PUT: 수정(update)
 * DELETE: 삭제(delete)
 */

// 서버만들기
// import express from 'express';
// const app = express();
// app.listen(8080);
// 서버 만들기 끝
728x90
반응형
LIST

'JavaScript > Node.js' 카테고리의 다른 글

Node.js - get  (0) 2023.04.24
Node.js - express  (0) 2023.04.24
Node.js - json  (0) 2023.04.24
Node.js - template  (0) 2023.04.24
Node.js - http  (0) 2023.04.24
728x90
반응형
SMALL

이 코드는 Node.js의 http 모듈을 사용하여 간단한 REST API 서버를 만드는 예제.

먼저 skills 배열. 이 배열은 /skills 엔드포인트에 대한 GET 요청을 처리할 때 반환되는 데이터.

그리고 http.createServer() 메서드를 사용하여 HTTP 서버를 생성. 이 메서드는 콜백 함수를 인수로 받으며, 이 함수는 요청(request)이 들어오면 호출.

콜백 함수에서는 요청이 GET인지 POST인지를 확인하고, 각각에 대한 처리를 수행. GET 요청인 경우에는 skills 배열을 JSON 형태로 반환. POST 요청인 경우에는 요청 본문을 받아들이고, JSON으로 파싱하여 skills 배열에 추가.

마지막으로, server.listen() 메서드를 사용하여 서버를 실행. 이 메서드는 서버가 사용할 포트 번호를 인수로 받는다. 이 예제에서는 8080번 포트를 사용 포트번호는 변경 가능.

728x90
반응형
LIST

'JavaScript > Node.js' 카테고리의 다른 글

Node.js - express  (0) 2023.04.24
Node.js - Rest, API, RestAPI, 서버만들기  (0) 2023.04.24
Node.js - template  (0) 2023.04.24
Node.js - http  (0) 2023.04.24
Node.js - 폴더 정리 프로그램  (0) 2023.04.24
728x90
반응형
SMALL

 

728x90
반응형
LIST

'JavaScript > Node.js' 카테고리의 다른 글

Node.js - Rest, API, RestAPI, 서버만들기  (0) 2023.04.24
Node.js - json  (0) 2023.04.24
Node.js - http  (0) 2023.04.24
Node.js - 폴더 정리 프로그램  (0) 2023.04.24
Node.js - npm  (0) 2023.04.24
728x90
반응형
SMALL

728x90
반응형
LIST

'JavaScript > Node.js' 카테고리의 다른 글

Node.js - json  (0) 2023.04.24
Node.js - template  (0) 2023.04.24
Node.js - 폴더 정리 프로그램  (0) 2023.04.24
Node.js - npm  (0) 2023.04.24
Node.js - pipe  (0) 2023.04.24
728x90
반응형
SMALL
const path = require('path');
const os = require('os');
const fs = require('fs');

const curdir = process.argv[2];
const workingDir = path.join(__dirname, curdir);
console.log(workingDir)

if(!curdir || !fs.existsSync(workingDir)) {
    console.error('해당 경로 및 폴더가 존재하지 않습니다.');
    return;
}

const videoDir = path.join(workingDir, 'video');
const capturedDir = path.join(workingDir, 'captured');
const duplicatedDir = path.join(workingDir, 'duplicated');

!fs.existsSync(videoDir) && fs.mkdirSync(videoDir);
!fs.existsSync(capturedDir) && fs.mkdirSync(capturedDir);
!fs.existsSync(duplicatedDir) && fs.mkdirSync(duplicatedDir);

fs.promises
    .readdir(workingDir)
    .then( processFiles )
    .catch(console.error);

function processFiles(files) {
    files.forEach((file) => {
        if ( isVideoFile(file)){
            move(file, videoDir)
        }else if (isCapturedFile(file)){
            move(file, capturedDir)
        }else if (isDuplicatedFile(file)){
            move(file, duplicatedDir);
        }
    });
};

function isVideoFile(file) {
    // g: global, 전역검색
    // m: multi-line 다중, 여러줄 검색
    const regExp = /(mp4|mov)$/gm;
    const match = file.match(regExp);
    return !!match; // 해당 값이 존재할 때 true 아니면 false;
}

function isCapturedFile(file) {
    const regExp = /(png|aae)/gm;
    const match  = file.match(regExp);
    return !!match;
}

function isDuplicatedFile(file) {
    if(!file.startsWith('IMG_') || file.startsWith('IMG_E')) {
        return false;
    }
    return true;
}

function move(file, targetDir){
    console.info(`move ${file} ot ${path.basename(targetDir)}`)
    const oldPath = path.join(workingDir, file);
    const newPath = path.join(targetDir, file);
    fs.promises
        .rename(oldPath, newPath)
        .catch(console.error);
}
  1. 주어진 경로의 유효성을 체크하고, 필요한 디렉토리를 생성.
  2. fs.promises.readdir() 메서드를 이용하여 해당 경로의 파일 리스트를 읽어옴.
  3. 읽어온 파일 리스트를 processFiles() 함수를 통해 처리.
  4. 파일의 확장자를 검사하여, 각각 isVideoFile(), isCapturedFile(), isDuplicatedFile() 함수를 통해 분류.
  5. move() 함수를 이용하여 분류된 파일을 해당하는 디렉토리로 이동.
  6. 각각의 분류 조건을 만족하지 못한 파일은 이동하지 않음.
  • path: 파일 경로 조작과 관련된 기능을 제공하는 모듈.
  • os: 운영체제와 관련된 기능을 제공하는 모듈.
  • fs: 파일 시스템과 관련된 기능을 제공하는 모듈. promises 속성을 이용하여 비동기적으로 파일 시스템 작업을 수행할 수 있습니다.
728x90
반응형
LIST

'JavaScript > Node.js' 카테고리의 다른 글

Node.js - template  (0) 2023.04.24
Node.js - http  (0) 2023.04.24
Node.js - npm  (0) 2023.04.24
Node.js - pipe  (0) 2023.04.24
Node.js - stream  (0) 2023.04.24
728x90
반응형
SMALL
// 노드 프로젝트 매니저라는 뜻 python에서 pip랑 동일
/** package.json 만들기
 * npm init
 * npm init -y : 기본 설정으로 만들기
 * name, version, description, entry point, test command, git repository(깃 허브), keywords(이프로젝트의 주제), author(지은이), license(어디까지 법적인 효력을 가지게 할건지), OK?
 *
 *  npm install, npm i
 *
 * json에 scripts 부분에 추가
 * //npm start라는 명령어를 쳤을때 node 1-npm을 쳐주겠다.
 * 임의로 만든 명령어같은경우 npm run을 붙여주어야 실행 가능
 * npm install express
 * express에 필요한 부분들이 package lock에 들어가게됨
 * 실제로 서버에 올릴때는 node_modules파일은 같이 안올리고, package, lock.json파일만 같이 올려준다.
 *
 * nodemon 설치하기
 * npm i nodemon --save-dev
 * "start": "nodemon 1-npm",
728x90
반응형
LIST

'JavaScript > Node.js' 카테고리의 다른 글

Node.js - http  (0) 2023.04.24
Node.js - 폴더 정리 프로그램  (0) 2023.04.24
Node.js - pipe  (0) 2023.04.24
Node.js - stream  (0) 2023.04.24
Node.js - buffer  (0) 2023.04.24

+ Recent posts