Skip to content

Instantly share code, notes, and snippets.

@SSTPIERRE2
Last active December 23, 2016 14:49
Show Gist options
  • Save SSTPIERRE2/e124710cfe58329d8772c14c597c4a4c to your computer and use it in GitHub Desktop.
Save SSTPIERRE2/e124710cfe58329d8772c14c597c4a4c to your computer and use it in GitHub Desktop.

Getting Started With Node.js and REDCap:

Node.js is a JavaScript runtime environment which lets you run JavaScript outside of a Web browser.
  • It is primarily used to create lightweight, scaleable Web servers, which is what we're going to do right now!
  • This is particularly useful as a server-side solution for receiving/responding to HTTP requests.
  • Let's start by installing Node and NPM (node package manager, a public registry of installable node modules).
1. Try visiting the Node Homepage and grabbing the latest version. Once it installs, let's run a couple commands.

Assuming we're developing on OSX, open up Terminal and run:

  $which node
    'which' checks that the following argument is in your environment variables. You can also run:
  $node -v
    This will tell you which version of node installed, just to be sure.
2. Use your favorite text editor, such as Atom, to create an empty file named something like nodeServer.js, and write the following code:
//Lets require/import the HTTP module
var http = require('http');

//Lets define a port we want to listen to
const PORT=8080; 

//We need a function which handles requests and send response
function handleRequest(request, response){
    response.end('It Works!! Path Hit: ' + request.url);
}
//Create a server
var server = http.createServer(handleRequest);

//Lets start our server
server.listen(PORT, function(){
    //Callback triggered when server is successfully listening. Hurray!
    console.log("Server listening on: http://localhost:%s", PORT);
});
3. Now, save your changes, open Terminal back up, and change directories to where your file is located.

The server object you instantiated will begin listening on the desired port as soon as you run the file like so:

  $node nodeServer.js
You should see a message to confirm that your server is listening.
4. Open up your favorite Web browser and type into the URL search bar:
  localhost:8080
You should see the text returned from your program.
5. Relevancy to my work at Partner's:

We are working on a project involving patient demographic and health data gathered through devices such as FitBit. This data will be stored in a REDCap project, like a web-based spreadsheet, but with an API through which external software can programmatically store and/or download data. A unique token, along with the project URL, allows us to run a JavaScript file and export the desired data via simple HTTP requests.

To add more granular context, exports will be done as a JSON object parsed into an array. Using an npm module, that object will be converted into CSV format (spreadsheet format) and appended to a larger CSV file. This file will ultimately be used as the training set for a machine learning algorithm.

The REDCap Record Export Script

I wrote this script in Python as we were crunched for time and needed to produce something that would get our records exported in CSV format. As the JavaScript package I planned to use to send HTTP requests was proving to be more time-costly than Python, it's lucky that you can just call and run a script from a JavaScript file.

import requests
import csv
import json
from requests import post

TOKEN = 'Hidden'
URL= 'https://redcap.partners.org/redcap/api/'

payload = {
    'token': TOKEN,
    'format': 'json',
    'content': 'record',
    'type': 'flat'

}
response = post(URL,data=payload)
all_records = response.json()
print response.status_code
print len(all_records)
single_record = all_records[0]
for single_record in all_records:
    for field_name, value in single_record.items():
        with open('allRWJRecords.csv', 'a') as csvfile:
            recordWriter = csv.writer(csvfile, delimiter=' ')
            recordWriter.writerow("%s: %s" % (field_name, value))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment