Getting Started With Node.js and Geddy

Getting Started With Node.js and Geddy

Tutorial Details
  • Topic: Node.js, Geddy, npm, JavaScript
  • Difficulty: Easy
  • Estimated Completion Time: 30 minutes

In this three part tutorial series, we’ll be diving deep into the process of creating a to-do list management app in Node.js and Geddy from scratch. In this introductory article, we’ll review how to install Node.js on Windows and OS X, getting Geddy installed, and generating our first app. Ready?


What is Node?

If you’ve been developing web apps for the last couple of years, you’re likely already familiar with Node.js, but let’s go over it – just in case you’re new to the scene.

Node.js is a platform built on Chrome’s JavaScript runtime for easily building applications in JavaScript that run on the server. Node.js uses an event-driven, non-blocking I/O model, which makes it perfect for building real time apps.


What is Geddy?

Geddy should feel very familiar to you.

Geddy is a simple and structured MVC (model, view, controller) framework for Node.js. You can use it to quickly create web apps and JSON APIs. If you’ve done any level of work with Ruby on Rails or PHP’s CodeIgniter, Geddy should feel very familiar to you; it’s got a restful router, template rendering, controllers, and models.


Installing Node.js

Node.js runs on Windows, OS X, and Linux. I’ll show you how to get set up on both Windows and OS X. if you’re on Linux I’ll assume that you’ve got Node installed, know how to get it installed, or know someone that can help you with the process.

First, go to http://nodejs.org and click the download button. Find the installer link for your operating system, and download it. Follow the installation prompt to get installed. If you’re on Windows, you may need to reboot your computer to get the ‘node’ command on to your path.

You should now have both Node and npm (Node Package Manager) installed.


Installing Geddy with npm

Node has a great package manager built right in. It’s called, npm, and, as of this writing, there are nearly 8,000 packages available. Check out http://toolbox.no.de to browse through them if you’d like. For this tutorial, however, we’ll use npm to install Geddy (our framework) and Jake (the build tool that Geddy uses):

Jake is a JavaScript build program for Node.js.

  • Open up your terminal
  • type npm install -g geddy jake

That’s it! Now that you’ve got Geddy installed, let’s see about generating your first app.


Generating a Geddy App

Geddy uses a global executable to generate apps/resources, and to start up your app server. This will all take place on the command line, so open up your terminal again. Before we generate our app, let’s cd to a good location to store your app. This can be anywhere on your machine, though, I prefer to do my development in my ~/dev/ directory.

cd path/to/the/place/you/code

Next, we’ll use geddy to generate our app structure. We’ll be creating a to-do application, so we’ll call ours, todo_app

geddy app todo_app

All done. Now what did that do for us?


An Overview of Our Generated App

If you take a look within the newly created todo_app directory, you’ll see that Geddy has generated a fair bit of code for you. Your directory structure should look a bit like this:

  • app/
    • controllers/
    • models/
    • views/
  • config/
  • lib/
  • log/
  • node_modules/
  • public/

Let’s step through these one by one:

app: Here’s where most of the magic happens. Most of your app’s logic will be located in one of the three directories contained in this one.

app/controllers: All of your app’s controllers (the part that ties your models to your views) go here. You’ll also notice that there’s already two controller files in there: application.js (which all controllers inherit from) and main.js (the controller that ties your / route to your app/views/main/index.html.ejs template).

app/models: Here’s where you’ll be storing your models – there’s nothing in there yet, but we’ll adding one in during the next tutorial.

app/views: All of your app’s templates are stored here. For now, you’ll see that you have an application.html.ejs file in the layouts directory – this file is the main template for your app, all of your front-end wrapper code should go in here. You should also have an index.html.ejs file in the main directory. This is what get’s rendered by the main controller’s index action when you hit the / route.

config: The configuration files for your app goes here. You should have the development.js, production.js, environment.js, router.js and init.js files in there. The init.js file is a file that runs just after the app gets started, before any requests come in. This can be used to add functions and properties that need to be app-wide. The router.js file is used to create routes for your application. Routes tie URLs to controller actions. For global settings, you’ll want to edit the environment.js file. For production and development settings, edit the corresponding config files.

lib: This is the place where you can put any file’s that you’d like to use all over your app.

log: All of your logs will be stored here. You should get an access.log, a stdout.log, and a stderr.log after you run your app.

node_modules: This is where the modules that you install will be stored. Think of it as a lib for other people’s code.

public: Finally, here’s where all of your front end specific stuff will live. All you css files, images, and front-end js files will be in here. You’ll notice that Twitter’s bootstrap and jQuery come pre-packaged with all Geddy apps.


Starting Up Your New Geddy App

Now that we have an app generated, I’ll demonstrate how to start it up. First, open the terminal again, and navigate to your app’s directory:

cd ~/path/to/code/todo_app

Once you’re there, start the app up by using the geddy command:

geddy

You should see some output that looks a bit like this:

Now that we’ve started up the server, go ahead and check it out in browser. Visit http://localhost:4000, and take a look!

Bonus: Because Geddy uses Bootstrap out of the box, with it’s responsive layout enabled, our app will immediately display nicely in a mobile browser. Resize your browser window to verify this.


The Next Step

This concludes the first part of our tutorial series on Node.js and Geddy. In the next one, I’ll demonstrate how to generate a todo resource (which will give us a better base to build our app upon), and go into the details of building a real app with Geddy. If you have any questions, feel free to leave a comment here or open an issue on GitHub. Stay tuned!

Note: Want to add some source code? Type <pre><code> before it and </code></pre> after it. Find out more
  • http://www.udgwebdev.com Caio Ribeiro Pereira

    On my blog I have some posts about Node.js, there is a curious post about Express x Geddy:

    http://www.udgwebdev.com/express-vs-geddy/

    I hope you enjoy it even my blog being in portuguese hehehe

    • Ariunbat

      Gonna read your blog. No worries about Portuguese when you have Google Translate

      • http://crpwebdev.com Caio Ribeiro Pereira

        Thanks!

        On my blog http://www.udgwebdev.com there are a lot of post about Node.js too.

    • http://yammer.com/jobs Daniel Erickson
      Author

      Geddy has gone through a bit of a rewrite since then, so you might find that it’s a bit more performant now. It does automatic process clustering, something you would have to do yourself with Express. This is definitely a good article though, thanks for the writeup!

      • http://www.udgwebdev.com Caio Ribeiro Pereira

        I didn’t know about the automatic process clustering, it seems very useful and there is nothing on another node modules with the same ease which Geddy has.

        I’ve been studying the Geddy and there are a lot of stuff good to build a big and modular webapp.

        I have only one question, Is there some examples using Mocha testing a Geddy webapp?

  • Seed

    Nice! I’m waiting for next part :)

  • Ariunbat

    Thanks. Looks forward to the next part soon.

  • http://www.michaelchromik.com mchchrmk

    Nice and easy start…great. Looking forward to the next part!

    • http://yammer.com/jobs Daniel Erickson
      Author

      Yeah, this one is the easiest of the three ;) Looking forward to your feedback on the next ones!

  • http://www.clifu.net Thanish

    Can’t wait till next 2 parts.

    • http://yammer.com/jobs Daniel Erickson
      Author

      Thanks! Let me know if you have any questions.

  • http://www.lotusmarketing.ca Lotus Marketing

    Node.js is so promising. I really hope it blooms someday.

  • coredump

    Great introduction! Eagerly waiting for next parts :)

  • Adem

    I hope that this series will be completed — unlike the other one from last year. Looking forward to this!

    Will we also use a database like MongoDB? I am playing around with node.js but struggling with connecting to a database — Heroku Support also does not seem to have an answer. :( So a part about connecting to a database would also be nice!

    • http://yammer.com/jobs Daniel Erickson
      Author

      Yep, the third tutorial is all about persisting data to MongoDB using Geddy and Node. Heroku has some gotchas with Geddy, but if you run into any issues, submit an issue on http://github.com/mde/geddy and we’ll help you through it.

  • Deacs

    Awesome, thank you!

  • http://pressedweb.com Cory

    Ohhhh I’m excited about some node.js tutorials… :-o

  • http://www.novainspire.com alex

    awesome post. love the idea of building node.js app for high performance and agile web apps.

  • http://www.amitavroy.com/justread Amitav Roy

    Hey Daniel, nice article. Waiting for the next one in this series.

  • IE

    Good job! Very nice tut!

    I hope there will be more Node.js tutorials in the future.

    One last thing, maybe you can record a video for the next parts instead of writing it? :)

  • Monk

    I’m not sure if this is appropriate for this article, but I’m having a trouble. I installed node.js on Windows Vista and it seems to be successful, but when I try the command like “node –version” or “npm install less”, the answer is always “…”, three dot or period and stop at there (doesn’t show any error message either.)

    Could anyone help me? I’ve tackled with this problem on days and I couldn’t find any good answer on the Net so far.

    • http://yammer.com/jobs Daniel Erickson
      Author

      Have you tried restarting your computer yet? Sometimes that path gets a little screwed up and you have to restart to get it to take effect. Let me know if this doesn’t solve it.

  • Bay

    I hope you will keep up the same or similar structure on the next parts because this one was a breeze to read. :) Thanks for the great article.

    • http://yammer.com/jobs Daniel Erickson
      Author

      While the next two articles are a bit more challenging than this one, it certainly tries to keep the same pace and cadence. Thanks for the feedback!

  • Tschello

    How can you setup production environment for Heroku and Geddy app? “heroku config:add NODE_ENV=production” doesnt work for me.

    • http://yammer.com/jobs Daniel Erickson
      Author

      If you’d like to start geddy up in production use `geddy -e production` instead of just `geddy`. That will use your production config file instead of your development one.

  • MarshallChen

    Good Job!

  • http://www.flerekunder.no Eirik

    Is this the same tutorial as posted on : http://geddyjs.org/tutorial.html ?

    • http://yammer.com/jobs Daniel Erickson
      Author

      This series is a much more detailed version of that one. It will show you how to use form partials, share them between views, method overrides, and it will show you how to persist your data in MongoDB.

  • Nicolas Schneider

    Well, basically the same as http://geddyjs.org/tutorial.html

  • chichibek

    i’m trieng to learn and mvc project in node but why Geddy?

    • http://yammer.com/jobs Daniel Erickson
      Author

      Good question. Geddy is designed to get you up and running quickly, with as little setup and hassle as possible. It’s built from the ground up, so it doesn’t depend on Connect, Express, or any other framework. This means that you get a cohesive system that wasn’t hacked together out of a bunch of modules.

      I talk a little bit more about this here: http://nodeup.com/fourteen

  • Rafael

    There are some other options build on top of express that I prefer: http://towerjs.org/ and http://railwayjs.com/

    • http://yammer.com/jobs Daniel Erickson
      Author

      Both of those are pretty good projects. TowerJS is built on CoffeeScript, and it’s pretty hard to use it with plain old JavaScript. So if you don’t want to learn/use CoffeeScript, I don’t recommend it.

      Both of these projects are built on top of express, which is a bit like trying to mod Sinatra into Rails- express wasn’t really designed for this kind of architecture.

  • http://arrowinthekn.ee Kristjan

    So, I thought I might go through the tutorial and learn something new, but I can not go past this: http://dl.dropbox.com/u/47663272/wellwellwell.png What’s all about? Could you explain this?

    • http://yammer.com/jobs Daniel Erickson
      Author

      Looks like you’re typing `nmd` instead of `npm` – hope that helps!

  • http://www.istudio.com.mx Alfredo Ramirez

    Tried other tutorials about Node.js in the past but this one made thinks a bit clearer. Thanks a lot!! Waiting for the next ones!!

  • Abdallah

    Thank you.
    It’s new for me but it like amazing.

  • Dan Smart

    Would you mind telling me how Geddy compares to Monorail.js (https://github.com/runexec/Monorail.js) ?

  • Monk

    Thanks for the reply. I’ve restarted many times since I installe it, but it didn’t solve the problem at all. I guess setting the languege as Japanese could make this situation.

    Anyway, this is such a great article!! I’m now learning CodeIgniter; I should try Getty next!!

    I’m looking forward to seeing your next article soon.

  • David

    When’s part 2 coming? =)

    • http://www.jeffrey-way.com Jeffrey Way

      This coming Tuesday.

  • http://www.techjot.com TechJot

    Can we have all 3of them NOW? ;-) Can’t wait.

  • Michael Grech

    You can have all 3? Doesn’t anyone read the manual? Ha no they don’t, see below.

    http://geddyjs.org/tutorial.html#show-todo

  • http://www.apogeum-pozycjonowanie.pl Pawel

    Looks quite simple, I have my localhost ready to go, waiting for the next part!

  • http://zilliontutorial.com Aditya Verma

    waiting for the rest..

  • Dan Smart

    Could you tell me how Geddy compares to Monorail.js (https://github.com/runexec/Monorail.js) ?

  • http://www.darasani.com mojo706

    Thank you for the tut. Just finished with the previous beginner tut on Node and Geddy is a good start. Tuesday has been marked on my Google Calendar.

  • http://ambuj.co.uk Ambuj Sinha

    Great article. Will you also be covering deploying a node.js app to production?

    • http://yammer.com/jobs Daniel Erickson
      Author

      Unfortunately, I couldn’t fit that into this series. Deploying a node app is pretty simple these days though, there are plenty of hosting services that provide node out of the box

      Joyent
      Heroku
      Nodejitsu

      Alternatively, you could rent a VPS from any number of hosting providers and set node up yourself.

  • Emil

    I have a problem when trying to install geddy. Everytime i try i get an 304 error on both geddy and jake.. Does anybody know what’s causing this?

    • http://yammer.com/jobs Daniel Erickson
      Author

      Thanks for trying Geddy, sorry you’re having issues – could you open an issue at http://github.com/mde/geddy? We’ll help you get all set up.

    • Coda Cat

      I don’t know what is causing this but I got this to work by typing the following:
      sudo npm install -g geddy (asks for your password to install)
      sudo npm install -g jake (installs without asking for your password)

  • http://rawdesigns.net Rob

    Hey thanks a bunch for this! I had just compiled my first Sencha Touch app through the command line following their docs, and now this! Came together a lot quicker than anticipated, so I’m stoked for the next tut :-)

  • http://victorbello.com Victor Bello

    Can I deploy an application that is built with Geddy to Heroku just like any other Node.js application? What do I need to do different?

  • Ed Pelesh

    Does it work with bootstrap .less files or just css. I wonder if it’s possible to compile .less server-side automatically.

    • http://yammer.com/jobs Daniel Erickson
      Author

      You can definitely do that, it’s just not baked in yet. We’re working on an automatic build step to help make this easier.

  • http://boletobest.com aaditya

    just come to know about node.js . does it can work with php applications??

  • prabhjeet singh

    need to know where and how do you deploy node/geddy apps?

  • http://dari.us.lt Darius

    Awesome :)

  • http://ajwebdesigner.in Ajinkya

    wah mast

  • Liguminy ru

    Geddy installed correctly and create app, but after launching i recive an error
    http://stackoverflow.com/questions/15317451/cant-create-geddy-js-app-from-tutorial
    and there is no JS file inside app directory like app.js or smth else