반응형
반응형

moment 사용하기

# 폴더 생성 및 열기
mkdir moment_practice
cd moment_practice

# package.json 초기화
npm init -y

# moment.js 설치
npm install moment

현재 날짜: moment()

//현재 날짜: moment()
// moment_practice.js

const moment = require("moment");

// 현재 날짜
console.log("========== today ==========");
console.log(moment());

특정 날짜 지정: moment('date')

// 특정 날짜 지정: moment('date')
// 특정 날짜 지정
console.log("========== specific date ==========");
console.log(moment("2021-01-27", "YYYY-MM-DD")); // Moment<2021-01-27T00:00:00+09:00>

형식 지정: format()

// 형식 지정: format()
// 현재 날짜 형식 지정
console.log("========== format ==========");
let date = moment("2021-01-27");
// 년-월-일
console.log(date.format("YYYY-MM-DD")); // 2021-01-27
// 시:분:초
console.log(date.format("HH:mm:ss")); // 00:00:00
// 요일
console.log(date.format("dddd")); // Wednesday
// 년-월-일 요일
console.log(date.format("YYYY-MM-DD dddd")); // 2021-01-27
// 년-월-일 시:분:초
console.log(date.format("YYYY-MM-DD HH:mm:ss")); // 2021-01-27 00:00:00
// 년-월-일 요일 시:분:초
console.log(date.format("YYYY-MM-DD dddd HH:mm:ss")); // 2021-01-27 Wednesday 00:00:00
// 날짜 더하거나 빼기: add(), subtract()
// 날짜 더하거나 빼기
console.log("========== add or subtract day, month, year ==========");
console.log(moment("2021-01-27").add(1, "days")); // 2021-01-28
console.log(moment("2021-01-27").add(2, "months")); // 2021-03-27
console.log(moment("2021-01-27").add(2, "years")); // 2023-01-27
console.log(moment("2021-01-27").subtract(1, "days")); // 2021-01-26
console.log(moment("2021-01-27").subtract(2, "months")); // 2020-11-27
console.log(moment("2021-01-27").subtract(2, "years")); // 2019-01-27

날짜 더하거나 뺄 때 생길 수 있는 문제점
한 moment 변수를 기준으로 날짜를 연속적으로 더하거나 빼게 되면 해당 변수도 add or subtract 함수를 실행하는 도중 값이 변하게 된다.

// 날짜 더하거나 뺄 때 생길 수 있는 문제점
// 한 moment 변수를 기준으로 날짜를 연속적으로 더하거나 빼게 되면 해당 변수도 add or subtract 함수를 실행하는 도중 값이 변하게 된다.

// 날짜 더하고 뺄 때 문제점
console.log("========== problems when adding or subtracting dates ==========");
let now = moment("2021-01-27");
console.log(now.add(3, "days")); // 2021-01-30
console.log(now.subtract(5, "days")); // 2021-01-25
console.log(now); // 2021-01-25

// 해결 방법: clone()
//add or subtract를 하기 전에 clone() 함수를 사용하면 된다.

// 해결 방법
console.log("========== solutions to the above problems ==========");
let fixedNow = moment("2021-01-27");
console.log(fixedNow.clone().add(3, "days")); // 2021-01-30
console.log(fixedNow.clone().subtract(5, "days")); // 2021-01-22
console.log(fixedNow); // 2021-01-27

https://millo-l.github.io/Nodejs-moment-%EC%82%AC%EC%9A%A9%ED%95%98%EA%B8%B0/

 

[Node.js] moment 사용하기

nodejs에서 moment.js를 사용해보자.

millo-L.github.io

 

반응형
반응형

Node.js | split() function

function splitStr(str) {
     
    // Function to split string
    var string = str.split("*");
     
    console.log(string);
}
 
// Initialize string
var str = "Welcome*to*GeeksforGeeks";
 
// Function call
splitStr(str);
반응형
반응형

jQuery  id like  찾기  

<script>
    //id가 testid로 시작하는 엘리먼트들 접근
    $("[id^='testid']")
    
    //id가 testid로 끝나는 엘리먼트들 접근
    $("[id$='testid']")
</script>
반응형
반응형

메일 수신확인 원리

1. 수신자가 메일을 읽음

2. 메일 HTML내 img이미지 정보를 읽으려고 자사 내 주소 접속

3. 자사 내 페이지 작동

4. DB업데이트

 

-- 잡코리아
<div style="display:none;">
	<img src="http://imail3.jobkorea.co.kr:8888/trace/openChecker.jsp?mailidx=메일PK&amp;seqidx=347404&amp;service=22&amp;dmidx=0&amp;emidx=0&amp;duration=0&amp;site=0" border="0" width="0" height="0" loading="lazy">
</div>


-- 스티비
<img src="https://event.stibee.com/v2/open/회원을 구분할수 있는 주소" width="0" height="0" style="height:0px;max-height:0px;border-width:0px;border-color:initial;line-height:0px;font-size:0px;overflow:hidden;" loading="lazy">

 

참조 : https://hoyashu.tistory.com/70

반응형
반응형

[Node.js]  Path : 파일 경로를 다루고 변경하는 유틸리티가 포함되어 있다

https://nodejs.sideeffect.kr/docs/v0.10.0/api/path.html

 

Path Node.js v0.10.0 Manual & Documentation

Path# Stability: 3 - Stable This module contains utilities for handling and transforming file paths. Almost all these methods perform only string transformations. The file system is not consulted to check whether paths are valid. Use require('path') to use

nodejs.sideeffect.kr

path.normalize(p)#

'..'와 '.' 부분을 처리해서 문자열 경로를 정규화한다.

슬래시가 여러 개 있는 경우 슬래시 하나로 교체하고 경로의 마지막에 슬래시가 있는 경우에는 유지한다. windows에서는 역슬래시를 사용한다.

path.normalize('/foo/bar//baz/asdf/quux/..')
// returns
'/foo/bar/baz/asdf'

path.join([path1], [path2], [...])#

모든 아규먼트를 합쳐서 최종 경로로 정규화한다. 문자열이 아닌 아규먼트는 무시한다.

path.join('/foo', 'bar', 'baz/asdf', 'quux', '..')
// returns
'/foo/bar/baz/asdf'

path.join('foo', {}, 'bar')
// returns
'foo/bar'

path.resolve([from ...], to)#

to를 절대경로로 변환한다.

to가 절대경로가 아니면 절대경로를 찾을 때까지 from 아규먼트들을 우측에서 좌측의 순서로 앞에 이어붙힌다.모든 from 경로를 사용한 후에도 절대경로를 찾지 못하면 현재 워킹 디렉토리를 사용한다. 최종 경로는 정규화되고 경로가 루트 디렉토리로 처리되지 않는한 마지막 슬래시는 제거한다. 문자열이 아닌 아규먼트는 무시한다.

이는 쉘에서 cd 명령어를 순서대로 실행한 것과 같다.

path.resolve('foo/bar', '/tmp/file/', '..', 'a/../subfile')

//-- 이는 다음과 비슷하다.:

cd foo/bar
cd /tmp/file/
cd ..
cd a/../subfile
pwd

//--다른 경로라 존재할 필요가 없거나 파일일 수도 있다는 점만이 다르다.

path.resolve('/foo/bar', './baz')
// returns
'/foo/bar/baz'

path.resolve('/foo/bar', '/tmp/file/')
// returns
'/tmp/file'

path.resolve('wwwroot', 'static_files/png/', '../gif/image.gif')
// if currently in /home/myself/node, it returns
'/home/myself/node/wwwroot/static_files/gif/image.gif'

 

path.relative(from, to)#

from에서 to까지의 상대경로를 처리한다.

때로는 두 개의 절대경로를 가지고 있고 하나에서 다른 하나로의 상대경로를 얻어야 한다. 사실 이는 path.resolve의 반대 변환이다. 이 의미를 다음 예제에서 보자.

path.resolve(from, path.relative(from, to)) == path.resolve(to)

//-----------------------------------------------

path.relative('C:\\orandea\\test\\aaa', 'C:\\orandea\\impl\\bbb')
// returns
'..\\..\\impl\\bbb'

path.relative('/data/orandea/test/aaa', '/data/orandea/impl/bbb')
// returns
'../../impl/bbb'

path.dirname(p)#

경로의 디렉토리이름을 반환한다. Unix의 dirname 명령어와 비슷하다.

path.dirname('/foo/bar/baz/asdf/quux')
// returns
'/foo/bar/baz/asdf'

path.basename(p, [ext])#

경로의 마지막 부분을 반환한다. Unix의 basename 명령어와 비슷하다.

path.basename('/foo/bar/baz/asdf/quux.html')
// returns
'quux.html'

path.basename('/foo/bar/baz/asdf/quux.html', '.html')
// returns
'quux'

path.extname(p)#

경로의 마지막 부분의 문자열에서 마지막 '.'에서부터 경로의 확장자를 반환한다. 경로의 마지막 부분에 '.'가 없거나 첫 글자가 '.'이라면 빈 문자열을 반환한다. 

path.extname('index.html')
// returns
'.html'

path.extname('index.')
// returns
'.'

path.extname('index')
// returns
''

path.sep#

플랫폼의 파일 구분자. '\\'나 '/'이다.

'foo/bar/baz'.split(path.sep)
// returns
['foo', 'bar', 'baz']

 

path.delimiter#

플랫폼에 특화된 경로 구분자인 ;나 ':'이다.

 

console.log(process.env.PATH)
// '/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin'

process.env.PATH.split(path.delimiter)
// returns
['/usr/bin', '/bin', '/usr/sbin', '/sbin', '/usr/local/bin']

반응형
반응형

 

SyntaxError: await is only valid in async function

 

 

1. 에러메세지 : SyntaxError: await is only valid in async function

2. 원인 : async없이 await사용.

3. 해결 : await는 async 안에서만 사용할 수 있다. 

해결한 코드 

 

export const home = async (req, res) => {
  const videos = await Video.find({});
  res.render("home", { pageTitle: "Home", videos });
};

 

반응형

+ Recent posts