function aticleInsert(dbname,collection,document,callback){
var db = mongoClient.db(dbname);
db.collection(collection).insertOne(document).then((data)=>{
callback(data)
},(err) => {
callback(err)
})
}
function articleDelete(dbname,collection,sql,callback){
var db = mongoClient.db(dbname);
db.collection(collection).removeOne(sql).then(data => {
callback(data)
}).catch(error => {
callback(error)
})
}
function articleUpdate(dbname,collection,sql,update,callback){
var db = mongoClient.db(dbname);
db.collection(collection).updateOne(sql,update).then(data => {
callback(data)
}).catch(error => {
callback(error)
})
}
function handleInsert(req,res){
if(!req.body) return res.sendStatus(400);
let document = req.body.document;
let collection = '';
if(req.url === '/preview'){
collection = previewCollection;
}else if(req.url === '/article'){
collection = detailCollection
}
aticleInsert(dbname,collection,document,(data) => {
res.send(data)
return
})
}
function handleDelete(req,res){
if(!req.body) return res.sendStatus(400);
let document = req.body.document;
let sql = document['sql'],
collection = '';
if(req.url === '/preview'){
collection = previewCollection;
}else if(req.url === '/article'){
collection = detailCollection
}
articleDelete(dbname,collection,sql,(data) => {
res.send(data)
return
})
}
function handleUpdate(req,res){
if(!req.body) return res.sendStatus(400);
let document = req.body.document;
let sql = document['sql'],
update = document['update'],
collection = '';
if(req.url === '/preview'){
collection = previewCollection;
}else if(req.url === '/article'){
collection = detailCollection
}
articleUpdate(dbname,collection,sql,update,(data) => {
res.send(data)
return
})
}
app.post(/^\/preview$|^\/article$/,jsonParser,handleInsert)
app.delete(/^\/preview$|^\/article$/,jsonParser,handleDelete)
app.put(/^\/preview$|^\/article$/,jsonParser,handleUpdate)
console