Wednesday, November 11, 2015

Creating a Node.js web application from scratch Part 1



My team member, David and I submitted a web application for the Blumixathon. This was David's very first web application. He had many questions. To make things easier, I created step by step screenshots of the bare bones beginnings of our app and used them to explain the process to him. Like David, others could benefit from the step by step explaination.

We created our web application using Node.js, Express.js, Async, Jade, HTML, CSS, Bootstrap, Javascript, Cloudant NoSQL DB etc. 

So here is the step by step explaination.

Step 1 - Create Basic App

This code sample shows a very simple application server written using Express.js. The application server, when started, listens for HTTP requests at port 3000. When you access the application using a browser, it shoudl return the message 'Hello Prachi'.



You also need to write a package.json file. This is the configuration file for your Node.js application. It allows you to configure details such as the application name, version, description, how to start the application, the Node.js libraries that your application uses, the Node.js engine to use and so on.


First, run npm install to install the dependencies that your application needs. Next, run npm start to start the application.


Hitting the URL http://localhost:3000 in a browser, shows you the very first resopnse from your application.


Step 2 - Move Routing to Routes Directory

The next step is to move the routing to index.js and use the Express.js Router. Instead of telling your server what to do when a request for a certain path comes in, you tell your server where to find that information, i.e. index.js. Generally, the convention is to keep the routing files in the routes folder



At the moment, you only have one route. But you can add more as needed. To verify that the routing is working correctly and requests are being handled by Express.js, change the response message for the root path to 'Hello David!'.


Restart your application from command line and here's the result.



In the next blog post entry, I will show you how to add Jade views and CSS to your Node.js web application. Note that these blogs are meant to help you create a very basic web application - only the very basic requirements. For complex requirements, please refer to formal Node.js documentation.

No comments:

Post a Comment