개발/node.js
[node.js] CRUD : delete 기능 만들기
개발햄
2019. 1. 30. 21:34
// Assuming that 'path/file.txt' is a regular file.
fs.unlink('path/file.txt', (err) => {
if (err) throw err;
console.log('path/file.txt was deleted');
});
var fs = require('fs');
if (pathname === '/delete_process') {
var body = '';
// requst data를 통해서 데이터가 들어와 body에 저장
request.on('data',function(data){
body += data;
});
// 그 후 end`로 넘어와 post에 담아서 뽑아 쓸수 있다
request.on('end',function(){
// post에 body데이터가 저장됨
var post = qs.parse(body);
var id = post.id;
// fs.unlink를 통해 파일 삭제
fs.unlink(`data/${id}`, (err) => {
response.writeHead(302,{Location:`/`});
response.end();
})
});
파일 삭제 기능을 만들 때, 링크로 해서는 안됩니다.
버튼을 이용해서 post방식으로 보내야 합니다 .
fs.unlink(`data/${id}`, (err) => {
데이터 폴더 안에 post.id로 받은 파일 네임을 받아서 파일을 삭제합니다.