Created
March 24, 2015 13:09
-
-
Save nilasissen/63da5798c1c376c8e681 to your computer and use it in GitHub Desktop.
Simple API using express+mongodb
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
'use strict'; | |
var express = require('express'); | |
var bodyParser = require('body-parser'); | |
var mongoose = require('mongoose'); | |
var io = require('socket.io')(server); | |
var Schema = mongoose.Schema; | |
var fs = require('fs'); | |
var atob = require('atob'); | |
mongoose.connect('mongodb://localhost:27017'); | |
//var ctc = require('./model/ctc'); | |
var app = express(); | |
//app.use(express.errorHandler()); | |
app.use(bodyParser.urlencoded({extended: true})); | |
app.use(bodyParser.json()); | |
/*############DATA SCHEMAS########################################*/ | |
//data base schemas for all api | |
//schema for cregistration | |
var registrationSchema = new mongoose.Schema({ | |
fingerPrint_id: String, | |
timeStamp: String | |
}); | |
//schema for panic | |
var panicSchema = new mongoose.Schema({ | |
fingerPrint: String, | |
type: String, | |
timeStamp: String, | |
lattitude: String, | |
longitude: String, | |
message: String | |
}); | |
//schema for current | |
var currentSchema = new mongoose.Schema({ | |
fingerPrint: String, | |
timeStamp: String, | |
type: String | |
}); | |
//schema for tracking | |
var trackingSchema = new mongoose.Schema({ | |
fingerPrint: String, | |
lattitude: String, | |
longitude: String, | |
timeStamp: String | |
}); | |
//schema for complain | |
var complainSchema =new mongoose.Schema({ | |
fingerPrnt: String, | |
timeStamp: String, | |
latitude: String, | |
longitude: String, | |
message: String | |
}); | |
/*####################################################################*/ | |
/*########CREATING DATABASE MODELS####################################*/ | |
//creating a dabase model | |
//creating model for register | |
var rs = mongoose.model('registration',registrationSchema); | |
//creating mdoel for panic | |
var pn = mongoose.model('panic',panicSchema); | |
//creating mdoel for current | |
var cu = mongoose.model('current',currentSchema); | |
//creating mdoel for tracking | |
var trk = mongoose.model('tracking',trackingSchema); | |
//creating mdoel for complain | |
var cmpln = mongoose.model('complain',complainSchema); | |
/*######REQUEST #########################################*/ | |
app.get('/',function(req,res){ | |
res.send('/index.html'); | |
}); | |
app.get('/current',function(req,res){ | |
mongoose.model('current').find(function(err,current){ //accessing the database model using the .find() method | |
//console.log(req); | |
res.send(current); | |
}); | |
//res.send("/GET to /register"); | |
}); | |
app.post('/register', function(req, res){ | |
req.body.timeStamp = Math.floor(new Date()); | |
console.log(new Date(req.body.timeStamp)); | |
//for inserting data into database | |
rs.collection.insert(req.body, {} , function(){ | |
console.log(req.body); | |
}); | |
res.send('POST to /register'); | |
}); | |
//panic is used for panic button type=0 | |
app.get('/panic',function(req,res){ | |
//mongoose.model('panic').find(function(err,panic){ //accessing the database model using the .find() method | |
//res.write(JSON.stringify(panic)); | |
//}); | |
//res.send("/GET to /register"); | |
res.end(); | |
}); | |
app.post('/panic',function(req,res){ | |
req.body.timeStamp = Math.floor(new Date()); | |
console.log(new Date(Date.parse(req.body.timeStamp))); | |
//data inserting for panic | |
var typechk = req.body.panic[0]; | |
var emergencyTypes = ["Panic", "Woman", "Fire", "Medical", "Accident", "Complaint", "Resolved"]; | |
(typechk.type !== undefined)? typechk.type=emergencyTypes[parseInt(typechk.type)]:typechk.type='undefined'; | |
console.log(typechk.type); | |
pn.collection.insert(req.body, {}, function(){ | |
console.log(req.body); | |
}); | |
res.send('POST to /panic'); | |
}); | |
//complain is used for complain button | |
app.post('/complain',function(req,res){ | |
req.body.timeStamp = Math.floor(new Date()); | |
console.log(new Date(req.body.timeStamp)); | |
//data inserting for complain | |
cmpln.collection.insert(req.body, {}, function(){ | |
console.log(req.body); | |
}); | |
res.send('POST to/complain'); | |
}); | |
app.post('/photo', function(req,res){ | |
req.body.timeStamp = Math.floor(new Date()); | |
console.log(new Date(req.body.timeStamp)); | |
var Base64Data = req.body.photo[0]; | |
atob(Base64Data); | |
fs.writeFile('out.png',Base64Data,function(err){ | |
if(err) return console.log('error occured'); | |
}); | |
res.send('POST TO /compalin/photo'); | |
}); | |
//tracking | |
app.post('/tracking',function(req,res){ | |
req.body.timeStamp = Math.floor(new Date()); | |
console.log(new Date(req.body.timeStamp)); | |
//data inserting for tracking | |
trk.collection.insert(req.body, {}, function(){ | |
console.log(req.body); | |
}); | |
res.send('POST to /tracking') | |
}); | |
io.on('connection',function(socket){ | |
console.log('connection started'); | |
var stream = mongoose.model('panic').find().limit(10).stream(); | |
stream.on('data',function(tweets){io.socket.emit('data',tweets);}); | |
}); | |
var server = app.listen(3002, function() { | |
var host = server.address().address | |
var port = server.address().port | |
console.log("Server running at http://%s:%s", host, port); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment