Skip to content

Instantly share code, notes, and snippets.

@eliantor
Last active August 29, 2015 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eliantor/bec7002b1b9d74cdc6e6 to your computer and use it in GitHub Desktop.
Save eliantor/bec7002b1b9d74cdc6e6 to your computer and use it in GitHub Desktop.
esempio di init script
// 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