Web application in Node.js – painful beginning

I want to try something new. I’m developing web applications for over ten years now using ColdFusion. I’m not telling that this is boring, I just want to try something new, something that can be used also in other fields. I was also using PHP previously and as per today I’m adjusting PHP sites from time to time so this language doesn’t seem to be “new” to me. Python and Node.js were considered by me – Python because this one is easy to use also for Linux scripting, Node.js because it uses JavaScript which is more or less used on the sites I’m working with. JavaScript is the must-have for me one way or another, so why not to try Node.js?

Choosing the proper starting point

I took a look at the basic information about Node and I found that it can react to the client HTTP requests without any web server daemon between the code and the user. Wonderful, but this also means that you have to handle the whole request acting as a server. You have to decide what should you do with the request for /homepage/ and what to do with the request for /login/ page. I’m not talking about the page business logic itself, I’m talking about handling the request before it will be sent to the particular piece of code to be processed. I don’t want to deal with this. Fortunately, there is a solution, not even one, but I chose Express which is described as web application framework for Node.js. It can handle routing (sending particular URLs to the particular pieces of code), static content delivery, error handling and much more. Beautiful, I thought, this is a good starting point for me.

Next choices to come

I’m aware that I should not place too much code in one file – I want to divide it (ideally) into some kind of controllers, views, static content such as CSS files and client-side JavaScript… This means that even though Express is handling much work for me, I still have something to do. There is Express generator which can generate base application structure for me. Let’s take a look at command line options… You can use ejs, hbs, hjs, jade, pug, twig, wash as your view engine… Beautiful, but I know nothing about them and I have to choose…

After few dozens of minutes spent on investigations I’m still not sure which one I want to use. I can not see the ideal candidate, but what I’m sure is that pug and jade are not my favorites because they are using their own notation for HTML elements – I’m not sure if I can handle that much right now. It’s hard to tell which one is “industry standard” or which one is mostly used. Most likely I will stay with EJS or HBS which is short for Handlebars as far as I can tell. They look fine for me for now. Will I regret my decision in the future?

Boilerplates?

Maybe the best solution for me will be to find a proper boilerplate website and start on such a foundation? There are few of them listed in the Express wiki on GitHub. One more hour spent on reviewing them and I’m sure that this is too much for me for now. They are prepared for someone who already knows what to look for. There are solutions chosen by the authors of these and I’m not sure if the same is good for me. It looks like I will have to learn much more than I expected. How to handle sessions, how to deal with secured and public content, how to persist the data in the database…

Is Mongo the only choice?

Looking at various tutorials, boilerplate websites, and sample projects on GitHub I came to the conclusion that there is nothing but Mongo when talking about a database for Node. I don’t want to say that you have no choice. There are libraries to connect to all main SQL database engines. The problem is that majority of examples and tutorials is based on Mongo. In my first application, however, I don’t want to use NoSQL database. An amount and type of data I want to compare are not well suitable for NoSQL solutions I guess. Maybe I’m wrong but I want to start without one more new thing to learn and to install. MySQL is my database of choice because on the server I want to use, it is already running.

Beginners conclusion

This was a busy day. I spend few hours in total digging in various places trying to find answers for beginners questions. I decided to start small and simple site using Express generator and just try to handle other problems once I will reach them. Wish me luck 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *