Building My First Node.js and Express Application
So I was working on my Ubuntu servers this weekend, trying to get some extra work in—the kind of work that doesn't actually feel like work. I started thinking about how natural and intuitive an asynchronous, event-driven application design might be when built with a language like JavaScript. I was sort of redesigning the BPM (Business Process Management) systems we worked on at Disney in my head, and I wanted to see what the modern JavaScript ecosystem had to offer.
The de facto server-side engine for JavaScript right now is Node.js. With just a few simple commands, I was able to launch my very first web application using Node and the Express framework.
Setting Up Node.js and Express on Ubuntu
If you're eager to get your hands dirty with server-side JavaScript, the setup process is remarkably straightforward. Here is the exact sequence of terminal commands I used to install the necessary packages and generate the initial application structure on my Ubuntu environment:
sudo apt-get install nodejs
sudo apt-get install npm
npm install -g express
express --sessions --css stylus --ejs myapp
cd myapp && npm install
node app
Once you run these commands, if you point your browser to http://localhost:3000/, you will see your brand new "Hello World" application up and running. It really is that easy to get a baseline server operational.
Exploring the EJS Templating Engine
Now, one of the first things I noticed about the default Express setup was the templating system it uses, called EJS (Embedded JavaScript). The good news for developers coming from a traditional web development background is that, at first glance, EJS looks and feels pretty much like PHP!
Here is an example of what the routing file looks like in /routes/index.js:
/*
* GET home page.
*/
exports.index = function(req, res){
res.render('index', { title: 'Express' });
};
And here is the corresponding view file located at /views/index.ejs:
<title> <%= title %> </title>
<ul>
<% for(var i=0; i<supplies.length; i++) {%>
<li><%= supplies[i] %></li>
<% } %>
</ul>
Okay, so the templating system looks simple enough to master quickly. The syntax is clean, and injecting dynamic JavaScript variables into HTML is practically seamless.
Job Scheduling in Node.js
The next thing I needed for my project was a way to implement robust job scheduling functionality. Basically, I was looking for a Node.js clone of the Quartz job scheduler that I had previously used in Grails.
I did a quick Google search and discovered that there is a popular GitHub project specifically designed for this purpose. However, this is pretty much where I ran out of steam in my initial investigation for the weekend. I concluded that I would look into the node-cron package later when I had more time to dive into its scheduling syntax and execution models.
The Future of Full-Stack JavaScript
Honestly, I think a technology solution presented on this platform can be rather elegant! I've been pushing the idea of a unified front-end and back-end language—and the seamless synchronization it provides—for a long time in my casual conversations here and there. None of my employers have had a project like this for me in the past, but the paradigm shift is undeniable.
By using the same language across the entire stack, development teams can reduce context switching, share validation logic, and streamline their deployment pipelines. If I can find a decent IDE that plays nicely with Node.js and Express, I just might try building something more substantial out of this prototype.
Frequently Asked Questions (FAQ)
What is Node.js?
Node.js is an open-source, cross-platform JavaScript runtime environment that executes JavaScript code outside a web browser. It is commonly used for building fast, scalable server-side applications.
What is Express.js used for?
Express is a minimal and flexible Node.js web application framework that provides a robust set of features for web and mobile applications, making it easier to handle routing and server architecture.
What is EJS in Node.js?
EJS, or Embedded JavaScript, is a simple templating language that lets you generate HTML markup with plain JavaScript, allowing you to easily insert dynamic data into your web pages.
