Here i will post series of articles about how to make simple web app in Clojure. I plan to talk about how to start building web app, how to upload it to heroku, how to upload images, use ClojureScript and so on. I am not very experienced programmer and worked mostly with ror framework so any comments and suggestions are welcome.
These notes mostly for beginners like i am. Some day i decided to build prototype for my web project and try new thing like Clojure for it, that was pretty yeasy and joyful.
It took me about 2 weeks to build clojure web app.
You have many options to start web app now days. Some of my favourites:
I worked with ror mostly and just tried to make very simple app with others.
Opinionated note.
They are fine frameworks and languges but all of them praise OOP paradigm. I have no big experience with programming but i can say that this concept is overcomplicated from my point of view. I even think that people just didn’t understand objects concept and started using it in another way, mostly wrong way. I could say that nowadays it’s Class programming.
“Classes orginize your code” ! I saw this phrase on some experienced programmer blog and it really is, that how most people think about this. You have to architect classes and hierarchies of classes to make things work but what about objects ? Any way you have to learn how this stuff works and it takes time, in some cases a lot of it.
Nowadays you can choose between complexity and simplicty. Long long ago was born Lisp. I don’t know why but it was mostly forgotten for years till these days. Clojure mostly reincarnation of this simple and powerfull language and concepts behind that. Now i understand why scheme is the language that people learn at computer since course in MIT. It simple and powerfull and it’s ideal for beginners.
You can learn most vital concepts in Clojure in a day. Syntax in an hour. And you can start doing things rightaway. So did i. I used only functions. No classes no variables no state no class hierarchies and thinking how this stuff works and why.
For simple web app i choosed “Noir web framework”. Most recent version 1.3.0-beta10 as of time this writing. It lucks good documentation and examples but is enough for start.
In order to compile and run your app you also need Leiningen. I used 2 version.
So what you need to do.
lein new noir mywebapp

It using noir template for the project.
$ lein new [TEMPLATE] NAME # generate a new project skeleton
We get simple structure for our web app and can run it.
lein run

Open browser and point it to
localhost:8080
And here it is:

You cann’t find this page in created project it uses ‘noir.content.getting-started file in Noir framework which you can find here. There is another good example of Noir web app - Noir blog it helped me a lot from the beginning. All we have in the project and that page you can edit is welcome.clj which uses common.clj as template. So point your browser to:
localhost:8080/welcome
and you can see:
Welcome to mywebapp
Let’s take a look at our welcome.clj file.
Here we defined namespace
mywebapp.views.welcome
which is our file actually and used :require to get our common.clj template that defines html structure for our site. Also we required ‘noir.content.getting-started to represent main page localhost:8080/. Finally we used only one macro from noir.core - defpage.
Let’s use defpartial macro. Add defpartial here:
(:use [noir.core :only [defpage defpartial] ] )
And here is our code.
Important notice. You don’t need to recompile app ( i mean stop server and start lein run again) to see the changes. Just add code and refresh the browser. I cann’t say for sure so far when recompile really needed but if you get an error or don’t see changes try recompile the project (Ctrl C and lein run). I had to do this in my project not so often.
You can import all macros from noir.core via (:use [noir.core]) but i recommend to use only those that you really need.
So what we can see here and learn. We don’t have monolitic framework here with tons of middleware and stuff loaded, we just using stuff that we really need and include tools that we really use. From that point of view Clojure app highly composable, something like lego.
In the next episode i will show how to upload app to heroku and ClojureScript support support to the project.
Also in other episodes i will talk about how to upload images to the project and use “fetch” library to make your app Ajaxable or one page app which is very convenient from my point of view. And of course about persistance to DB. I used MongoDB and Monger as convenient lib for persistence tasks and for querying.