Created
March 31, 2021 17:09
-
-
Save samuchakraborty/4cd89d9aca2969643c539b1e848b5e14 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const express = require('express'); | |
const mongoose = require('mongoose'); | |
const bodyParser = require('body-Parser'); | |
const ejs = require('ejs'); | |
const app = express(); | |
app.set('view engine', 'ejs'); | |
app.use(express.static("public")); | |
app.use(bodyParser.urlencoded({ | |
extended: true | |
})); | |
mongoose.connect("mongodb://localhost:27017/wikkiDB", | |
{ useNewUrlParser: true, useUnifiedTopology: true }); | |
const articlesSchema = { | |
title: { | |
type: String, | |
required: true | |
}, | |
content: { | |
type: String, | |
required: true | |
}, | |
}; | |
const Article = mongoose.model('articles', articlesSchema); | |
// app.get('/article', function (req, res) { | |
// Article.find({}, function (err, articleList) { | |
// if (!err) { | |
// res.send(articleList); | |
// } | |
// else { | |
// res.send(err); | |
// } | |
// }); | |
// }); | |
// app.get('/article/:articleTitle', function (req, res) { | |
// Article.findOne({ title: req.params.articleTitle }, function (err, article) { | |
// if (!err) { | |
// res.send(article); | |
// } | |
// else { | |
// res.send(err); | |
// } | |
// }) | |
// }); | |
app.route('/article').get(function (req, res) { | |
Article.find({}, function (err, articleList) { | |
if (!err) { | |
res.send(articleList); | |
} | |
else { | |
res.send(err); | |
} | |
}); | |
}); | |
app.route('/articles').post(function (req, res) { | |
const article = new Article({ | |
title: req.body.titles, | |
content: req.body.contents | |
}); | |
console.log(req.params.titles); | |
console.log(article); | |
article.save(function (err) { | |
if (!err) { | |
res.send("succesfully added item"); | |
} | |
else { | |
//console.log('/////'); | |
//console.log(err); | |
res.send(err.message); | |
} | |
}); | |
}); | |
app.route('/delete').delete(function (req, res) { | |
Article.deleteMany({}, function (err,) { | |
if (!err) { | |
res.send("succcesfully delete all articles"); | |
} | |
else { | |
res.send(err.message); | |
} | |
}); | |
}); | |
app.route('/article/:articleTitle').get(function (req, res) { | |
Article.findOne({ title: req.params.articleTitle }, function (err, article) { | |
if (article) { | |
res.send(article); | |
} | |
else { | |
res.send("there is no article with this " + req.params.articleTitle); | |
} | |
}); | |
}); | |
app.route('/article/:articleTitle').put(function (req, res) { | |
const articleTitle = req.params.articleTitle; | |
Article.update({ title: req.params.articleTitle }, | |
{ title: req.body.titles, content: req.body.contents }, | |
{ overwrite: true }, | |
function (err) { | |
if (!err) { | |
res.send("Sucessfully added"); | |
} | |
else { | |
res.send(err.message); | |
} | |
} | |
); | |
}).patch(function (req, res) { | |
Article.update({ title: req.params.articleTitle }, { $set: req.body }, | |
function (err) { | |
if (!err) { | |
console.log(req.body); | |
res.send("Sucessfully added"); | |
} | |
else { | |
res.send(err.message); | |
} | |
} | |
); | |
}).delete(function (req, res) { | |
Article.deleteOne({ title: req.params.articleTitle }, function (err) { | |
if (!err) { | |
res.send("successfully deleted "+ req.params.articleTitle); | |
} | |
else{ | |
res.send(err.message); | |
} | |
}); | |
}); | |
app.listen(3000, function () { | |
console.log("server is working"); | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment