Last active
August 29, 2015 14:03
-
-
Save eliantor/bec7002b1b9d74cdc6e6 to your computer and use it in GitHub Desktop.
esempio di init script
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
// esempio di auto init per il server | |
// helpers.js | |
on('install',function(e){ | |
try{ | |
['beverages','food','orders'].forEach(function(c){ | |
_command({resource: 'collections', name: 'post', params: c}); | |
}); | |
} catch(e){ | |
return { | |
status: false, message: 'failed' | |
}; | |
} | |
// restituendo false lo script non viene effettivamente salvato | |
return {status: false, message: 'collections created'}; | |
}); | |
var randInt = require('utils.module').randInt; | |
function getDoucments(coll){ | |
var docs = _command({ | |
resource: 'documents', | |
name: 'list', | |
params: {collection: coll} | |
}); | |
} | |
on('request',function(evt){ | |
if(evt.data.method === 'GET') { | |
var docs = getDocuments('testCollection'); | |
if (docs.length == 0){ | |
return {status: 404, content: 'no documents'}; | |
} else { | |
return {status: 200, content: docs[randInt(docs.length)]}; | |
} | |
} | |
}); | |
on('request',function(evt) { | |
if(evt.data.method === ‘GET’) { | |
var docs = getDocuments(‘testCollection’); | |
if(docs.length == 0) { | |
return {status: 404, content: ‘no documents’}; | |
} else { | |
return {status: 200, content: docs[randInt(docs.length)]}; | |
} | |
} else { | |
return {status: 404, content: ‘wooops!!!’}; | |
} | |
}); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment