REST api using mongodb + coffeescript
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.
[{"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 = \"\"\[email protected] ?= \"stopped\"\[email protected] = {\n \"amsterdam\": \"http://frontend-notificusweb.rhcloud.com\"\n}\n\[email protected] = (succes,msg) =>\n status = if succes then \"OK \" else \"ERROR\"\n console.info \"EHB \"[email protected]+\" \"+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\[email protected] = 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 = \"\"\[email protected] ?= \"stopped\"\[email protected] = {\n \"amsterdam\": \"http://frontend-notificusweb.rhcloud.com\"\n}\n\[email protected] = (succes,msg) =>\n status = if succes then \"OK \" else \"ERROR\"\n console.info \"EHB \"[email protected]+\" \"+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\[email protected] = 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\[email protected] = (succes,msg) =>\n status = if succes then \"OK \" else \"ERROR\"\n console.info \"API \"[email protected]+\" \"[email protected]+\" => \"+msg \[email protected] = (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 \[email protected] = 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":[]}]