googlecreativelab / coder

A simple way to make web stuff on Raspberry Pi
http://goo.gl/coder
Apache License 2.0
2.42k stars 275 forks source link

Writing Node.js in Coder #47

Open brannondorsey opened 11 years ago

brannondorsey commented 11 years ago

I am a bit puzzled as to how writing Node.js inside of Coder works, where it is executed, and how it is debugged. When you use the fourth "Node" tab on any Coder app when will that server side code be run? If I am building an app in coder that relies on various Node.js processes (i.e. using the Twitter module to access a twitter stream and potentially push the content to the view) should that logic be written in the Node tab in the Coder GUI or inside the apps app.js file?

I am assuming that the request to each app's index page is handled via the exports.index_handler = function( req, res ) inside its respective app.js file. Does this mean that is where my custom Node code should go if I want it to be run whenever a user visits the page? What if I want it to be persistent?

Also, obviously Coder comes with Node because, well, Coder uses it. But I have found that I cannot access Node anywhere on my Pi except for inside coder-base. If I navigate to my home/ directory and try node somefile.js I get a command not found error. Is there a way to use the Node that comes with Coder on the Pi so that I don't have to install another copy?

mrutgerj commented 11 years ago

I was really confused on all of this too, and spent a great deal of time and frustration "reverse engineering".... then again, I am new to node.js so that is to be expected. But I have answers!

The node.js server side code will run when called by a javascript function, in the JS tab. Basically they are ajax calls. To understand how that works, I would first suggest to install an application that uses that functionality. Pity that coder wasn't released with an app that used it. This is the resource you want, install, get it running: http://www.codeproject.com/Articles/653230/Raspberry-Pi-Episode-3-Get-Coding-With-Coder

And yes, the node.js tab writes to app.js, one and the same.

Now, coder uses a very old (meaning a few months?) version of node, and they used a version where the binary is called nodejs. It is is located in /user/bin/. So you could navigate to your home dir and execute nodejs.You can verify the location by typing which nodejs.

brannondorsey commented 11 years ago

Wow! This is incredibly helpful! nodejs instead of node seems like what my issue was too. Haven't gotten a chance to test it yet though. Thanks!

jmstriegel commented 10 years ago

Sorry for the slow response. I need to put a howto together for node.js coding on coder (still working on similar for front-end starters for the really new users).

Coder is basically using express, and passing off the request and response objects to any handler that you 'register' in your get_routes or post_routes arrays.

So say you have an app called foo. It's default url is /app/foo/ In the node.js file for foo (which is actually saved in app.js when you do an export), you'll see this default route in get_handlers:

{ path:'/', handler:'index_handler'}

The / matches the final slash in the url /app/foo[/]. When coder gets that url, it sees that there is a registered handler called "index_handler" and it calls that method in your app with the request and response objects.

exports.index_handler = function( req, res ) {
    var tmplvars = {};
    tmplvars['static_url'] = exports.settings.staticurl;
    tmplvars['app_name'] = exports.settings.appname;
    tmplvars['app_url'] = exports.settings.appurl;
    tmplvars['device_name'] = exports.settings.device_name;

    res.render( exports.settings.viewpath + '/index', tmplvars );
};

That sets up a few variables that get rendered into your app's html file. If you look in the header of the html file you'll see some mustache replacements where those variables are injected.

Now let's say you want to make a little JSON api. Add this to the get_handlers list:

{ path:'/api/bar', handler:'api_bar_handler'}

Now when you hit the url /app/foo/api/bar, the api_bar_handler method will get called. Here's an example:

exports.api_wifi_configure_handler = function( req, res ) {
    res.json({
        message: 'you win'
    });
};

Hope this helps!

Oh, by the way. Here's a secret. If you want to look at how some apps with a back end work. Everything in coder, from the main UI to the editor, is a coder app.

Want to see how the main screen works, go to /app/editor/edit/coder Edit the editor? /app/editor/edit/editor Now... I really recommend not actually editing or saving anything there, or you may break something you need :)

mrutgerj commented 10 years ago

Thank you, does help, and puts the right words in my mouth.

Of course I'm going to say.... how about some more secrets, like a demo app, that doesn't load by default. It woud offer full GPIO access. That would be a real secret.

Actually it would be a real treat. I will report anything I can make progress on in that respect, but it won't be a node GPIO module.

Well... node-ffi, using javascript to call Cxx libs.