-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathweb.js
51 lines (40 loc) · 1.15 KB
/
web.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
var connect = require( 'connect' );
var mongo = require( 'mongodb' );
var port = process.env.PORT || 3000;
var mongoUri = process.env.MONGOLAB_URI;
var database = null;
var contactsCollection = null;
var contacts = {
add: function( firstName, lastName, requestCallback )
{
if ( contactsCollection != null )
{
contactsCollection.insert( { "firstName": firstName, "lastName": lastName }, function(error, result)
{
requestCallback( null, "success" );
});
}
}
};
mongo.connect( mongoUri, {}, dbConnectCallback );
connect.createServer(
require( 'connect-jsonrpc' )( contacts )
).listen( port );
function dbConnectCallback( error, db )
{
database = db;
database.addListener( "error", handleError );
database.createCollection( "contacts", createCollectionCallback );
};
function handleError( error )
{
console.log( "Error connecting to MongoLab" );
};
function createCollectionCallback( error, collection )
{
database.collection( "contacts", collectionCallback )
};
function collectionCallback( error, collection )
{
contactsCollection = collection;
};