Skip to content

Instantly share code, notes, and snippets.

@thekoushik
Last active November 3, 2018 10:24
Show Gist options
  • Save thekoushik/81c7d3da7e9ed10140812fe1b937a0e7 to your computer and use it in GitHub Desktop.
Save thekoushik/81c7d3da7e9ed10140812fe1b937a0e7 to your computer and use it in GitHub Desktop.
Simple MongoDB REST API
# Logs
logs
*.log
npm-debug.log*
# Runtime data
pids
*.pid
*.seed
# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov
# Coverage directory used by tools like istanbul
coverage
# nyc test coverage
.nyc_output
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt
# node-waf configuration
.lock-wscript
# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release
# Dependency directories
node_modules
jspm_packages
# Optional npm cache directory
.npm
# Optional REPL history
.node_repl_history
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var helmet = require('helmet');
app.use(helmet());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
//http://mongodb.github.io/node-mongodb-native/3.1/quick-start/quick-start/
const MongoClient = require('mongodb').MongoClient;
var db=null;
const client = new MongoClient('mongodb://localhost:27017',{ useNewUrlParser: true });
client.connect(function(err) {
if(err) return console.log(err);
console.log("Connected successfully to server");
db = client.db('mongoadmin');
});
var outputFn=(res)=>{
return (err,result)=>{
if(err) res.status(500).send(err);
else res.json(result);
};
}
app.get('/',(req,res)=>{
res.json({message:"Welcome to mongoadmin API"});
})
app.post('/insert/:name',(req,res)=>{
var col=db.collection(req.params.name);
if(Array.isArray(req.body))
col.insertMany(req.body,outputFn(res));
else
col.insertOne(req.body,outputFn(res))
})
app.get('/find/:name',(req,res)=>{
db.collection(req.params.name).find({}).toArray(outputFn(res))
})
app.post('/find/:name',(req,res)=>{
db.collection(req.params.name).find(req.body).toArray(outputFn(res))
})
app.post('/update/:name',(req,res)=>{
db.collection(req.params.name).update(req.body.select,req.body.set).toArray(outputFn(res))
})
app.get('/delete/:name',(req,res)=>{
db.collection(req.params.name).deleteMany({},outputFn(res))
})
app.post('/delete/:name',(req,res)=>{
var col=db.collection(req.params.name);
if(req.query.many)
col.deleteMany(req.body,outputFn(res))
else
col.deleteOne(req.body,outputFn(res))
})
app.get('/indexes/:name',(req,res)=>{
db.collection(req.params.name).indexes(outputFn(res))
})
app.get('/deleteindexes/:name',(req,res)=>{
db.collection(req.params.name).dropIndexes(outputFn(res))
})
app.post('/indexes/:name',(req,res)=>{
if(Array.isArray(req.body))
db.collection(req.params.name).createIndexes(req.body,outputFn(res));
else
db.collection(req.params.name).createIndex(req.body,outputFn(res));
})
app.get('/count/:name',(req,res)=>{
db.collection(req.params.name).countDocuments({},outputFn(res));
})
app.post('/count/:name',(req,res)=>{
db.collection(req.params.name).countDocuments(req.body,outputFn(res));
})
app.get('/collections',(req,res)=>{
db.listCollections({}).toArray(outputFn(res));
})
app.options("/*", function(req, res, next){
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET,POST,OPTIONS');
res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization, Content-Length, X-Requested-With');
res.json([
{method:"options",path:"/"},
{method:"post",path:'/insert/:name'},
{method:"get",path:'/find/:name'},
{method:"post",path:'/find/:name'},
{method:"post",path:'/update/:name'},
{method:"get",path:'/delete/:name'},
{method:"post",path:'/delete/:name'},
{method:"get",path:'/indexes/:name'},
{method:"get",path:'/deleteindexes/:name'},
{method:"post",path:'/indexes/:name'},
{method:"get",path:'/count/:name'},
{method:"post",path:'/count/:name'},
{method:"get",path:'/collections'},
]);
})
app.all('*',(req,res)=>{
res.status(404).end();
})
var port=3000;
app.listen(port,()=>{
console.log("Listening on 'http://127.0.0.1:"+port);
});
{
"name": "mongodb_admin_api",
"version": "0.0.1",
"description": "Simple mongodb rest api",
"main": "index.js",
"scripts": {
"start": "nodemon index"
},
"keywords": [],
"author": "Koushik Seal",
"license": "MIT",
"dependencies": {
"body-parser": "^1.15.2",
"express": "^4.14.0",
"helmet": "^3.6.0",
"mongodb": "^3.1.8"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment