Parse, NodeJS and CORS

A beautiful trio.

Eliseu C. Martinho
Oct 11, 2015 · 2 min read

This September, parse.com made a post on how to use nodejs with their javascript SDK. With this, one can develop on parse.com, using the javascript SDK and be able to debug the code, without doing the dreadful, code, deploy, run and watch the thousand lines of logs.

Actually you can run parse.com javascript SDK on nodejs for a long time. Yamill Vallecillo wrote a post this stuff. He has a small tutorial on how you can do it. The npm link for the parse library can be found here.

When I started working with parse.com, the first projects were very simple. Yet the more complex the projects got, the more the need to use other libraries increased. Parse.com already has a number of useful libraries such as moment.js and parse-image, to name a few. Yet they are either outdated or limited. Since I was in a javascript/nodejs environment, I started looking for libraries on npm. For a moment, it seemed cool, however there’s the dependencies issue. For standalone libraries, it is fairly easy to integrate them in a parse project. You just download it with npm and copy it into a folder of your choice. When there are other dependencies, you download all of them, fix their path and add them all to your project, or you can use browserify to have it all in a bundled file.

One of the issues with the projects that I have is the need to develop API’s that will be consumed by either mobile apps or web apps, served by a particular domain. Because of SOP (Single Origin Policy), this means I need to have a way to get around it. CORS (Cross-Origin Resource Sharing) is the answer. For those who are new to CORS, there’s this excellent website about it. Fortunately there’s this library for expressjs. Parse.com does not have CORS out of the box. Either you have to add the needed routing rules with the right headers to support it, or you can just download the library that does just that for you.

Let me take you step by step.

  1. Make sure you have parse installed. Just copy the command from here.

curl -s https://www.parse.com/downloads/cloud_code/installer.sh | sudo /bin/bash

2. Get the CORS library at

https://www.npmjs.com/package/cors

3. Copy the two files vary and cors: vary.js and cors.js.

4. Change the path on vary.js

to

5. Add these two lines where instantiated your expressjs app.

And voila. You have CORS. Happy coding.

Eliseu C. Martinho

Written by

suuuuper fahrrad byte surfer