Learn TypeScript by building a Reddit Api Client

TypeScript is seeing increasing adoption across the developer community. According to the Stack Overflow Developer Survey 2022 TypeScript ranks 3rd as the ‘Most Wanted’ programming language by developers.

It’s time for me to get up to speed and considering TypeScript is a superset of JavaScript the learning curve should be OK for me!

The best way to learn is to do right? With that in mind I’ve started a project to build a TypeScript Reddit Api Client. It’s pretty simple. You install the npm module and you can use it to interact with the Reddit API.

Now there is an official Reddit node module which last time I looked was written with JavaScript. So why not create a TypeScript version for learning?

Variety is the spice of life.

If you are interested in learning TypeScript feel free to catch up with me here.

Find out more here

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!