Created
February 19, 2013 11:47
-
-
Save joeriks/4985143 to your computer and use it in GitHub Desktop.
ExpressJs sample within Umbraco - using uRest to get (and possible to edit) Umbraco content , see http://joeriks.com/2013/02/18/fun-stuff-node-js-in-an-umbraco-site/
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
var express = require('express'); | |
app = express(); | |
uRestUser = { | |
Username: "admin", | |
Password: "xxxxxx", | |
Type: "user", | |
Token: "" | |
} | |
uRestServer = { | |
server : "localhost", | |
port : 57855, | |
path : "/rest" | |
} | |
var urest = require('./urest'); | |
app.get('/scripts/node/urest/documents/:id',urest.getDocument); | |
app.listen(process.env.PORT); |
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
var qs = require('querystring'); | |
var http = require('http'); | |
function requestWithToken(actualRequestCallback) { | |
if (uRestUser.Token == "") { | |
var userString = JSON.stringify(uRestUser); | |
var headers = { | |
'Content-Type': 'application/json', | |
'Content-Length': userString.length | |
}; | |
var options = { | |
port: uRestServer.port, | |
server: uRestServer.server, | |
path: '/rest/token', | |
method: 'POST', | |
headers: headers | |
}; | |
var requestToken = http.request(options, function (response) { | |
var str = ""; | |
response.on('data', function (data) { | |
str += data; | |
}); | |
response.on('end', function () { | |
var responseobject = JSON.parse(str); | |
if (typeof responseobject != undefined) { | |
uRestUser.Token = responseobject.Value; | |
actualRequestCallback(); | |
} | |
}) | |
}); | |
requestToken.write(userString); | |
requestToken.end(); | |
} else { | |
actualRequestCallback(); | |
} | |
} | |
function requestGetWithToken(resourceUrl, callback) { | |
requestWithToken(function () { | |
var authString = qs.stringify({ "auth_username": uRestUser.Username, | |
"auth_token": uRestUser.Token | |
}); | |
var headers = { | |
'Content-Type': 'application/json' | |
}; | |
var path = uRestServer.path + resourceUrl; | |
if (path.indexOf('?') == -1) { | |
path += "?"; | |
} else { | |
path += "&"; | |
} | |
path += authString; | |
var options = { | |
port: uRestServer.port, | |
server: uRestServer.server, | |
path: path, | |
method: 'GET', | |
headers: headers | |
}; | |
var request = http.request(options, function (response) { | |
var str = ""; | |
response.on("data", function (data) { | |
str += data; | |
}) | |
response.on("end", function (data) { | |
callback(str); | |
}) | |
}) | |
request.end(); | |
}) | |
} | |
exports.getDocument = function (req, res) { | |
var documentId = req.params.id; | |
requestGetWithToken("/documents/" + documentId, function (response) { | |
res.send(response); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment