Example REST api which uses mongodb + coffeescript to POST and GET a user. It uses a 'router' object so global functions can be used, instead of having seperate calls.
-
-
Save coderofsalvation/b21852df973d3f5c3919 to your computer and use it in GitHub Desktop.
REST api using mongodb + coffeescript
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
[{"id":"bba2cc5f.445d3","type":"mongodb","hostname":"127.13.155.130","port":"27017","db":"sb","name":"sb"},{"id":"69d2c39e.962d3c","type":"coffee","name":"post /api/v1/user","func":"req = msg.request\nif req.method is \"POST\" and req.url is \"/api/v1/user\"\n @context.global.log true, \"\"\n error = \"please provide email\" if not msg.req.body.email\n error = \"please provide fullname\" if not msg.req.body.fullname\n if error\n msg.payload = @context.global.payloadme {error:error}\n else\n msg.payload = @context.global.payloadme {}\n return [msg,msg.req.body]\n","outputs":"2","x":605,"y":75,"z":"3b3ff7f9.c4c008","wires":[["95556ae3.6aaa98"],["d9928df.f266d7"]]},{"id":"95556ae3.6aaa98","type":"http response","name":"","x":781,"y":48,"z":"3b3ff7f9.c4c008","wires":[]},{"id":"d9928df.f266d7","type":"mongodb out","mongodb":"bba2cc5f.445d3","name":"add user","collection":"user","payonly":false,"operation":"store","x":788,"y":99,"z":"3b3ff7f9.c4c008","wires":[]},{"id":"53934d43.ac6cb4","type":"coffee","name":"router","func":"@context.global.url = \"\"\n@context.global.status ?= \"stopped\"\n@context.global.cities = {\n \"amsterdam\": \"http://frontend-notificusweb.rhcloud.com\"\n}\n\n@context.global.log = (succes,msg) =>\n status = if succes then \"OK \" else \"ERROR\"\n console.info \"EHB \"+@context.global.url+\" \"+status + \" => \"+msg \n\n\nmsg.request = {};\nif msg.req\n\tmsg.request.ip = msg.req.headers['x-client-ip'];\n\tmsg.request.url = msg.req.url;\n\tmsg.request.method = msg.req.method;\n\tmsg.request.timestamp = new Date().getTime();\n\tmsg.request.payload = msg.req.body;\nelse\n\tmsg.request.url = msg.topic\n\n@context.global.url = msg.request.url\nconsole.info \"EHB request: \"+msg.request.url\n\nreturn msg;","outputs":1,"x":643,"y":641,"z":"3b3ff7f9.c4c008","wires":[[]]},{"id":"9eefcb9e.611038","type":"coffee","name":"router","func":"@context.global.url = \"\"\n@context.global.status ?= \"stopped\"\n@context.global.cities = {\n \"amsterdam\": \"http://frontend-notificusweb.rhcloud.com\"\n}\n\n@context.global.log = (succes,msg) =>\n status = if succes then \"OK \" else \"ERROR\"\n console.info \"EHB \"+@context.global.url+\" \"+status + \" => \"+msg \n\n\nmsg.request = {};\nif msg.req\n\tmsg.request.ip = msg.req.headers['x-client-ip'];\n\tmsg.request.url = msg.req.url;\n\tmsg.request.method = msg.req.method;\n\tmsg.request.timestamp = new Date().getTime();\n\tmsg.request.payload = msg.req.body;\nelse\n\tmsg.request.url = msg.topic\n\n@context.global.url = msg.request.url\nconsole.info \"EHB request: \"+msg.request.url\n\nreturn msg;","outputs":1,"x":643,"y":641,"z":"3b3ff7f9.c4c008","wires":[[]]},{"id":"98dc8f63.67237","type":"coffee","name":"router","func":"@context.global.req = false\n@context.global.log = (succes,msg) =>\n status = if succes then \"OK \" else \"ERROR\"\n console.info \"API \"+@context.global.req.method+\" \"+@context.global.req.url+\" => \"+msg \n@context.global.payloadme = (obj) =>\n payload = { \"ok\":true, \"msg\":\"OK\" }\n if obj.error\n payload.ok = false\n payload.msg = obj.error\n return JSON.stringify payload\n\nmsg.request = {};\nif msg.req\n\tmsg.request.ip = msg.req.headers['x-client-ip'];\n\tmsg.request.url = msg.req.url;\n\tmsg.request.method = msg.req.method;\n\tmsg.request.timestamp = new Date().getTime();\n\tmsg.request.payload = msg.req.body;\nelse\n msg.request = JSON.parse(msg.payload)\n msg.request.url = msg.topic\n \n@context.global.req = msg.request\n\nreturn msg;","outputs":1,"x":377,"y":264,"z":"3b3ff7f9.c4c008","wires":[["69d2c39e.962d3c","599395a9.a66c6c"]]},{"id":"1bacf277.e4530e","type":"inject","name":"","topic":"{'url':'/api/v1/user','method':'POST'}","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"x":107,"y":76,"z":"3b3ff7f9.c4c008","wires":[["98dc8f63.67237"]]},{"id":"5acbb3b1.a5344c","type":"http in","name":"","url":"/api/v1/user","method":"post","x":139,"y":76,"z":"3b3ff7f9.c4c008","wires":[["98dc8f63.67237"]]},{"id":"94c3bdbd.6b3c4","type":"inject","name":"","topic":"/api/v1/user","payload":"{\"method\":\"GET\"}","payloadType":"string","repeat":"","crontab":"","once":false,"x":120,"y":160,"z":"3b3ff7f9.c4c008","wires":[["98dc8f63.67237"]]},{"id":"7bb98179.84468","type":"http in","name":"","url":"/api/v1/user","method":"get","x":137,"y":160,"z":"3b3ff7f9.c4c008","wires":[["98dc8f63.67237"]]},{"id":"599395a9.a66c6c","type":"coffee","name":"get /api/v1/user","func":"req = msg.request\nif req.method is \"GET\" and req.url is \"/api/v1/user\"\n @context.global.log true, \"\"\n msg.payload = {} # find all\n return msg","outputs":"1","x":605,"y":166,"z":"3b3ff7f9.c4c008","wires":[["d832bb5a.27cd48"]]},{"id":"d832bb5a.27cd48","type":"mongodb in","mongodb":"bba2cc5f.445d3","name":"get users","collection":"user","x":763,"y":166,"z":"3b3ff7f9.c4c008","wires":[["e1ebcc56.1e143"]]},{"id":"e1ebcc56.1e143","type":"http response","name":"","x":899,"y":166,"z":"3b3ff7f9.c4c008","wires":[]}] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment