Coding - Algo/Nodejs

[Nodejs] file or symbolic link 삭제

jainn 2021. 6. 2. 22:45
728x90

fs.unlink(path, callback)

링크, 혹은 파일 삭제하는 함수

History

Asynchronously removes a file or symbolic link. No arguments other than a possible exception are given to the completion callback.

import { unlink } from 'fs';
// Assuming that 'path/file.txt' is a regular file.
unlink('path/file.txt', (err) => {
  if (err) throw err;
  console.log('path/file.txt was deleted');
});

fs.unlink() will not work on a directory, empty or otherwise. To remove a directory, use fs.rmdir().

 

 

https://nodejs.org/api/fs.html#fs_fs_unlink_path_callback

 

File system | Node.js v16.2.0 Documentation

 

nodejs.org

 

사용 예시

fs.unlink(`data/${id}`, function(error){
	response.writeHead(302, {Location: `/`}); //파일삭제와 동시에 홈으로 이동
	response.end('success');
})
반응형