Sunday, August 16, 2015

Node.Js

We are going to start nodeJs now i am lounch part one.

Node Basics

          Node.js is one of the biggest explosions in the past few years. Having the ability to run JavaScript (the clientside language many are familiar) on the server is an enticing notion.
Front-end developers that know JavaScript well can easily expand their knowledge to know back-end serverside code.

         Node is built on Google Chrome’s V8 JavaScript runtime and sits as the server-side platform.

Why Node?

            Node let’s us build real-time open APIs that we can consume and use with our applications. Transferring data or applications like chat systems, status updates, or almost any other scenario that requires quick display ofreal-time data is where Node does its best.

Some Example Node Uses

  • Chat client
  • Real-time user updates (like a Twitter feed)
  • RSS Feed
  • Online Shop
  • Polling App

Now that we have a brief overview of Node, let's dive in and build two applications:
  • A very simple app to show the basics of starting a Node project
  • A more fully-fledged application where we display popular Instagram pictures
Getting Started

Installing Node

                  Make sure you have Node (http://nodejs.org) and npm (Node's package manager) installed. npm comes bundled with Node so as long as you install Node, you'll have access to npm.

                  Once you've installed Node, make sure you have access to Node and npm from the command line. Let's verify installation by checking version numbers. Go into your command line application and type node -v and npm -v. You should see the following:

$ node -v
v0.12.0
$ npm -v
2.5.1

A Very Simple Node App

                        Node applications are configured within a file called package.json. You will need a package.json file for each project you create. This file is where you configure the name of your project, versions, repository, author, and the all important dependencies. Here is a sample package.json file:

{
"name": "node-app",
"version": "1.0.0",
"description": "The code repository for the Node booklet.",
"main": "server.js",
"repository": {
"type": "git",
"url": "https://github.com/scotch-io/node-booklet-code"
},
"dependencies": {
"express": "latest",
"mongoose": "latest"
},
"author": "Chris Sevilleja",
"license": "MIT",
"homepage": "https://scotch.io"
}

           That seems overwhelming at first, but if you take it line by line, you can see that a lot of the attributes created here make it easier for other developers to jump into the project. We'll look through all these different parts later in the book, but here's a very simple package.json with only the required parts.

{
"name": "node-booklet-code",
"main": "server.js"
}

These are the most basic required attributes

          main tells Node which file to use when we want to start our applications. We'll name that file server.js for all of our applications and that will be where we start our applications.

Initialize Node App

             The package.json file is how we will start every application. It can be hard to remember exactly what goes into a package.json file, so npm has created an easy to remember command that let's you build out your package.json file quickly and easily. That command is npm init.


Let's create a sample project and test out the npm init command.
  • Create a folder: mkdir awesome-test
  • Jump into that folder: cd awesome-test
  • Start our Node project: npm init.
       It will give you a few options that you can leave as default, blank, or customize as you wish. For now, you can leave everything default except for the main (entry point) file. Ours will be called server.js .

       You can see that our new package.json file is built and we have our first Node project!

        Since we have a package.json file now, we can go into our command line and type node server.js to start up this Node app! It will just throw an error since we haven't created the server.js file that we want to use to begin our Node application. Not very encouraging to see an error on our first time starting a Node server! Let's change that and make an application that does something.

      Now we will need to create the server.js file. The only thing we will do here is console.log out some information. console.log() is the way we dump information to our console. We're going to use it to send a message when we start up our Node app.

Here is our server.js file's contents.

console.log('Wow! A tiny Node app!');

       Now we can start up our Node application by going into our command line and typing: node server.js

$ node server.js
               We should see our message logged to the console. Remember that since Node is JavaScript on the server, the console will be on our server. This is in contrast with the client-side console that we'll find in our browser's dev tools.

Coming Soon NodeJs part two post.  

No comments:

Post a Comment

Swift Start Here