PhpRiot
News Archive
PhpRiot Newsletter
Your Email Address:

More information

Node.js with Express.js in Business

Note: This article was originally published at Planet PHP on 19 December 2012.
Planet PHP

The fundamental processes behind business and software development are very different, and it's often the reason why getting the two working together can be challenging.

In business, you're often able to test ideas quickly and without major costs, which means that an experimental approach to development is common. In software development, you aren't able to test your ideas as quickly, because you need things to be stable and that costs time and money. This means that software development depends on well-specified needs that business people have a hard time articulating, because they are not used to doing that.

There are many ways to go about improving this. One option is prototyping, and another option is for the business people to acquire a better understanding of how software development works, thereby improving the foundation for good communication.

I think learning Node.js with Express.js can be a great way for business people to gain an understanding of how software development works that lets them improve the relationship in two different ways: by building prototypes that can help them explain what they want, and by being better at speaking the language of developers.

Node.js

Node.js is JavaScript, and the very expressive nature of the language makes the code relatively easy to understand. This lets you learn as you go with a very limited understanding of the language. That might not be the optimal way to go about it, but it builds motivation that keeps you going.

It has a big and active community that builds awesome frameworks and modules, so when obstacles arise, chances are good that you can find a module or a fellow developer that can help you out. This keeps you moving forward.

Node can be used to a lot of other things too, like building a web or chat server, and that encourages you to try and understand how it all works together, and it makes you feel very empowered by just learning one language.

Express.js

Building web apps with Node lets you iterate and change the structure of your app very easily, because it is JavaScript on both the server side and the client side. Express.js is one of the most popular web app frameworks for Node, and it takes care of a lot of things while remaining light and flexible. This fits an experimental and iterative approach very well.

It's super easy to install, and it's amazing how quickly you can get an app running. The only steps you need to go through are1:

  • Install Express.js: npm install express
  • Generate the app: express myapp
  • Install dependencies: cd myapp && npm install
  • Run the app: node app
  • Access the app at: localhost:3000

Express defaults to using Jade, which is a template engine (in other words, a different way to write html and insert data into it) for Node. But, if you prefer using html and Handlebars.js, you can do that. First, install a few packages:

npm install html npm install hbs

Next, change some settings. In the app.js file, change app.set('view engine', 'jade') to app.set('view engine', 'html').

Then, configure the app to use hbs by adding this line:

app.engine('html', require('hbs').__express);

Now, rename index.jade to index.html and have it print the title with html and Handlebars.js:

{{title}}

Relaunch2 the app, go to localhost:3000, and it should display the title that is set in the route.

Development

The framework doesn't make any assumptions in terms of how you want to structure your code, so you're free to do what makes sense to you for the current project. That can be a bit confusing at first, so let's look at an example of how you'd begin the development of an app.

Let's say that you want to have a page that shows some numbers about your business. To handle that, you'll need a new route and a view, and you'll have to tell the app where you want the page to be.

  1. Add a numbers.js file to the routes directory, and paste in the code from the index route. Configure it to render the numbers view instead of the index view.
  2. Add a numbers.html file to the views directory.
  3. Add this line to your app.js file: app.get('/numbers', routes.nu

Truncated by Planet PHP, read more at the original (another 1981 bytes)