js.And i'm creating a simple blog in express.js. I use Typicode/lowdb as database , to create/update/delete, posts based on the id on a data.json file.And use the slug data in the json array to create/update/delete markdown posts with the Node.js File System API .On the delete side of the markdown posts i get an error that i don't understand.
{ [Error: ENOENT, unlink 'C:\Users\path\to\publicfolder\undefined.md']
errno: -4058,
code: 'ENOENT',
path: 'C:\\Users\\path\\to\\publicfolder\\undefined.md' }
I'm using fs.unlink like this to remove markdown posts:
var oldSlug = "public/"+req.params.slug+".md";
fs.unlink(oldSlug, function(err) {
if(err) {
console.log(err);
} else {
console.log("Deleted the old markdown file: " + oldSlug +"");
}
});
Here is some example of the code exposed by parts:
Example to create/update/delete Posts - Post.js
exports.index = function (req, res) {
res.render('post/index', { posts: db.list()});
};
exports.form = function (req, res) {
res.render('post/form');
};
exports.edit = function (req, res) {
var post = db.getById(req.params.id);
res.render('post/edit', {post: post})
}
exports.update = function (req, res) {
var slugTitle = slug(req.body.name).toLowerCase().split('.').join("");
var description = req.body.desc;
db.update({id: req.body.id, name: req.body.name, desc: req.body.desc,
slug: slugTitle});
//using this to rename and update markdown posts it work's
var oldPath = "public/"+req.body.slug+".md";
var newPath = "public/"+slugTitle+".md";
fs.unlink(oldPath, function(err) {
if(err) {
console.log(err);
} else {
console.log("Deleted the old markdown file : oldPath ");
}
});
fs.writeFile(newPath, description, function(err) {
if(err) {
console.log(err);
} else {
console.log("The post was updated as: newPath ");
}
});
res.redirect('/post');};
exports.remove = function (req, res) {
db.remove(req.params.id);
//using this to delete markdown posts i get the error
//changing req.params.slug to req.body.slug still gives the error
var oldSlug = "public/"+req.params.slug+".md";
fs.unlink(oldSlug, function(err) {
if(err) {
console.log(err);
} else {
console.log("Deleted the markdown file: " + oldSlug +"");
}
});
res.redirect('/post');};
Example on Server.js
app.get('/post', post.index);
app.get('/post/create', post.form);
app.post('/post/create', post.save);
app.get('/post/details/:id', post.details);
app.get('/post/edit/:id', post.edit);
app.post('/post/update', post.update);
app.get('/post/delete/:id', post.remove);
http.createServer(app).listen(app.get('port'), function(){
console.log("Express server listening on port " + app.get('port'));
});
Example of a index.jade
doctype
html
head
title
body
h1 Posts
ul
each item in posts
li Item #{item.name} - #{item.desc} - #{item.slug}
a(href='/post/details/#{item.id}') Details -
a(href='/post/edit/#{item.id}') Edit -
a(href='/post/delete/#{item.id}') Delete
div
p
a(href='post/create') Add new
The problem is on the exports.remove function in post.js file but why??I believe i'm identifying the markdown file to delete that corresponds to the Slug params from the document in the database or not? I Hope i'm making sense in the code.
The problem is that you are not passing the variable slug
anywhere to your remove
route. Since you are using GET it can't come from req.body, so you have to either pass it in the URL path or as query string.
Try changing these lines:
post.js
var oldSlug = "public/"+req.query.slug+".md";
index.jade
a(href='/post/delete/#{item.id}?slug=#{item.slug}') Delete
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.