Note that there are some explanatory texts on larger screens.

plurals
  1. POUnderstanding callbacks in Javascript and node.js
    text
    copied!<p>I'm a long time PHP (CodeIgniter &amp; WordPress) developer that only recently wanted to learn a few other languages. I've set out to learn Ruby (on Rails, and Sinatra), Python (w/ Flask framework) and Javascript with node.js. </p> <p>I decided to create the most basic application I can think of, a URL expander, using each of these languages. I have managed to create a working version in every language, except node.js and Javascript. </p> <p>I kinda know my problem, I know it is related to callbacks. I know I'm not doing it right. I get the basic idea of callbacks, but I just cannot figure out how to fix this mess I have created.</p> <p>This is my <a href="https://gist.github.com/1493486" rel="nofollow">whole code</a>:</p> <pre class="lang-js prettyprint-override"><code>var http = require('http'); var url = require('url'); function expand() { var short = url.parse('http://t.co/wbDrgquZ'); var options = { host: short.hostname, port: 80, path: short.pathname }; function longURL(response) { console.log(response.headers.location); } http.get(options, longURL); } function start() { function onRequest(request, response) { console.log("Request received."); response.writeHead(200, { "Content-Type": "text/plain" }); response.write("Hello World"); expand(); response.end(); } http.createServer(onRequest).listen(8888); console.log("Server has started."); } start(); </code></pre> <p>The server starts, and when a request is made, it calls the expand function which returns the expanded URL in the terminal. I'm trying to get it to print in the browser.</p> <p>Any help is appreciated. Thanks in advance.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload