Part 1: Create a simple blog with Node.js

Hi,

Let’s create a simple blogging system in Node.js.

For part 1 we’ll aim for Create, read, update for blog posts. No comments, authentication, administration just yet. We’ll save that for sebsequent posts.

What we need to begin

  • Well, we’ll need Node.js obviously. I’m assuming that you have this installed.
  • We will use the Geddy MVC framework to get us moving.
  • Database? Let’s use Mongo DB

Let’s go!

I work on a OSX so please tailor the steps to suit your operating system.

1) Install Geddy MVC
$ sudo npm install -g geddy
Notice the -g here, Geddy should installed globally. It gives command line tools for generating scaffolding and creating MVC parts.  Saves you the hassle, very RoR like here.

2) At this point you have Geddy installed.

Time to generate a skeleton Geddy Application. We’ll call our blog geddy_blog.

$ geddy app geddy_blog
3) Next generate scaffold for your blog posts:

Our blog will have the following fields:

  • ‘title’ which is the default the Geddy will show on the main blog page.
  • ‘body’ the actual text of the blog post
  • ‘status’ published or unpublished

$ geddy scaffold blog title:default body status

You should see the following output:

$ geddy scaffold blog title:default body status
[Added] app/models/blog.js
Creating table for Blog
[Added] test/blog.js
[Added] app/controllers/blogs.js
[Added] Resource blogs route added to config/router.js
[Added] View templates

4) Run geddy from the geddy_blog directory:

$ geddy

At this point you’ll have a CURD for the blog model you just created

Open your browser at http://localhost:4000/blogs

5) Database layer.Install MongoDB server if you haven’t already.

Skip this step if you have.

By default Geddy uses in-memory data storage but we want persistence and so we come to MongoDB.

Install MongoDB, remember I’m on OSX. See MongoDB for further instructions to suit your OS.

$ brew install mongodb

Start your MongoDB server:

$ mongod

7) Time to install mongodb node wrapper. This allows Node.js to talk to you mongoDB server.

$ npm install mongodb-wrapper

8) Configure Geddy to use MongoDB.

Open geddy_blog/config/development.js

Update defaultAdapter = ‘mongo’

Add db: { mongo: { dbname: ‘geddy_blog }

So you’ll development.js looks like this:


var config = {
detailedErrors: true
, debug: true
, hostname: null
, port: 4000
, model: {
defaultAdapter: 'mongo'
}
, db: {
mongo: {
dbname: 'blog_test'
}
}
, sessions: {
store: 'memory'
, key: 'sid'
, expiry: 14 * 24 * 60 * 60
}
};

module.exports = config;
9) Finally Ctrl-C on previous Geddy process and start it again:


$ geddy

Summary:

At this point you have created CRUD for your blog post model, stored in mongo db database.

Geddy is changing pretty pretty fast and this tutorial may be out of date. For more uptodate tutorials, docs please checkout the geddjys.org

For part 2 I hope to create comments for those blog posts.

Good luck!

One thought on “Part 1: Create a simple blog with Node.js”

  1. Awesome tutorial, I follow-up something like this and got great results, yes will be great to have the posts, for my case I was trying to link multiple nested models like [ Singer -> Album -> Songs ] but have not found to much documentation on how to achieve this.

    Keep doing the great job !!!
    Dino.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: