There is nothing I like more than bashing out code fast and not
having to be concerned about documentation or customer requirements or
other such necessary evils.
While I'm a Java hacker by
day, JavaScript is the code I
like to write. Its fun, its single thread is an enjoyable constraint that forces you to think a
little as you code. Multi-threaded JavaScript would be like playing
football but being allowed to use your hands.
This
month I've been permitted by my pay masters to run with a couple of little
projects written in
nodejs. Not doing big scalable web sockets things,
just quickly developing web apps that resolve specific problems, a REST
API, GUI that's shared across a few peeps in the team.
Nodetoy is a little project I wrote a while back when nodejs was only 0.5 versions old.
Nodetoy provides...
- A webserver for HTML and JS/CSS.
- Apache/nginx style Server Side Includes (SSI) for templating.
- A JSON/REST CRUD backend that saves flat files to the file system.
No code or config required, you turn it on and it start serving
your web pages and fetching and storing JSON data. It has no security,
no user/pass, no DDoS protection or anything like that, its a toy server
for Intranet use, rapid prototyping, web/desktop app development
with the browser as a GUI or just playing around with REST.
JSON layer is REST layer is so trivial its a joy to use.
- POST a blob of JSON to /data/foo.json to persist data.
- GET /data/foo.json to read data.
- DELETE /data/foo.json to delete data.
- GET /data/ returns a list of the .json files.
Thats it, there is no more to the backed.
Included
in the download is and old version of
jQuery, you probably want to
upgrade that, a little tester web page so you can get started GETing and
POSTing without having to write any JavaScript yet and a little TODO
app in case you need some boiler plate code to start with.
I like everything about this project.
It took no time to write, it was done as part of a series of introductions to nodejs we did in the Riereta hacklab in Barcelona.
It does one thing and it does one thing well, a Linux mantra.
It makes use of the right patterns in the right places.
Features are nicely encapsulated, which is easy with node.
It has tools instead of docs.
I'm quite proud of the logo, it conveys the simplicity.
It has no build process, its source code is both ready to run and ready to be hacked.
Originally
it had no dependencies other than node, because it was written back
when there weren't many modules and before npm was included with node.
Since then its been added to npm and various modules have been separated
out for re-use. You can still clone the github repo to get up and
running.
I've not touched the codebase for a while but when I got an opportunity to give it a run for its money recently I was pleasantly surprised.
With nodetoy is very simple to quickly build webapps that are fun to write, starting with the HTML.
It
has a very clear separation of front end and back end, and both are
easily hackable. Because the back end is so limited your forced to keep
it simple, which for RAD development soon pays off.
There
is no code in the BE that is tied to the front end, its the antitheses
of GWT. I write my FE code in jQuery but any ol' AJAX API would do.
The
older I get the more convinced I become of the fact that SSI is the
only templating that is needed, if you want to manipulate the DOM do so
in the browser.
One advantage of developing the BE in
nodejs is that its implicitly "discoverable" since everything is
delivered as code the application is the source code, there is no build
process just hack away and it boots so fast that [ctrl+c] [up] [enter]
is actually an acceptable way to work.
Of course being a
grownup language with node you also have unit testing, mocking, lint
for validating code code and a million and one bells and whistles that
you would expect with a professional development environment and that
you don't get if you do prototyping with CGI and bash. Despite all
the features node remains fun and easy to code. You can bounce between
working in unit tests on the CLI to running your web server for real,
literally, in a second.
In the last project I
knocked-up the JSON crud from nodetoy was sufficient to not need to make
any BE changes. This enabled me to focus on doing dev purely in Firefox
and notepad++. which brings me neatly to the next part of RAD
development.
jQuery / Bootstrap...
If your a pro HTML designer I'm sure you'll
cringe,
bootstrap is used by 1% of the web, its far from
original, but the fact is GUIs knocked up with bootstrap are very fast to
write and always end up looking decent. Arseing around with browser
incompatibilities is not a fun part of development. I write HTML for
Bootstrap in FireFox and it always workes in Chrome and Konqueor and the
time I had to support IE8 for one of my GUIs it all just worked, thank
you very much bootstrap.
Bootstrap doesn't focus much
on providing lots of features, it strikes me its a coders CSS tool. It
tries to have as little impact on your HTML as possible and lets you
focus on your code. The 960 grid system and responsive grid for me
have proven easily enough options for layout control. Sticking to a
tight grid format always helps to make pages look neat and tidy. Shame I
didn't know about it when I wrote nodetoy's test.html page.
Bootstrap does not require jQuery but it seems to be a natural pairing.
jQuery
is a coder friendly toolkit too. While it has a ton of plugins its core
focus is on helping you write code. Clearly no one wants to have to
write another date picker, however its not the widgets that makes jQuery
cool, its the code you end up writing. It is succinct. The selector
logic is as good as your HTML naming conventions are. jQuery does
nothing unexpected, it does exactly what you ask it to and no more.
jQuery does a wonderful job of making my code cross platform. I have
never had a problem with a bit of jQuery code not working where it
should. You say "jump" jQuery says "yes sir".
Comparing to the good ol'
lamp stack or
tomcat +
myslq I have to say that my nodetoy stack appeals to me more and more.
Pure
HTML5 wins over JSP/PHP the less code you write generating HTML the
better. Pulling request specific
pre-generated HTML from server-side is soo 1999.
JavaScript every where, is a very convenient way to work. Mixing data types,
formatting and conversions are annoyances to a coder. If your back end,
front end, data store and data transfer format are all talking the same
language you cut out a lot of crap from your code. You'll hear me
rant about JSON and how much I dislike it's use in Java & Python but when doing
RAD web GUIs its clearly the best fit for the job.
Schema
free Backends are much more convenient that relational databases. For
prototyping the benefit is immediate, and no Hibernate does not count.
If I needed to scale up to DB for some
reason a file system did not suffice for the job or the complete lack
of locking or versioning becomes a worry I'd drop in CouchDB and keep
the data in JSON. Going back to defining the data's detailed attributes
before you write the code seems so backwards. With schema free
development you want to add new attribute to your object you just
obj.newAtt = foo
and
thats it. No DDL, no size limits no datatype considerations or data
conversion considerations, if foo was a string well it stays a string
and if it was boolean it stays a boolean and if it was undefined good
job you didn't bother to create a column for it isn't it.
Bottom
line is that I've been able to turn around little dynamic webapps in
around 4 hours.
KISS principal has been working for me of late.