What is Nitrous and How can code on browser?

Hey guys,

I just uploaded a video on my channel, I tired to explained about one of the coolest cloud service that I have seen, it helps you to code on PHP,Node.Js and so many other languages just on your browser without installing any extra tools or application on your machine.

enjoy the video.

How can create Module in Node.js and reference them in any js file?

Hey guys,

Node.js same as other programming languages supports module. Basically modules help us to have better structure for our application and also it let you to maintain your application easily. For example you have an application that are doing these tasks:

1. Send email
2. Grab files from FTP
3. Create output files and upload into FTP
Each of these tasks can be a separate module, for those that are familiar with object oriented programing, module can play the same role as class. Module is not something new in Node.js but you must know how to define module and how import them to your app.js or anywhere that you want to use them.
Follow these few steps:
1. Create a js file in root folder. ( you need to have node folder, if you do not know what is node folder you can first look at this post)
2. Each module can have two types of function.
2.1. Public function which others can call this function and it is like interface for the module.
2.2. Private function which is used by other functions inside the module and others from outside cannot use it.
3. Copy can past these code inside the js file.

var name;




exports.getname = function(inputname){
     name=inputname;

}


exports.Printname = function(){
   console.log(name);

}

This is a very simple example of module, in this module we have a global variable which is name and two functions, first is getname which gets name and the other one is Printname which prints out the name. To make a function public we need to export it.

How can call a module?
It is very easy, first you need to import your module, in this example I want to import the module in app.js file but it can be imported in any other js file, based on your need. To impost any module you must use this command:


var testmodule = require("./getname.js");

require is something like import in vb.net or using in C#, if your module installed inside the node_modules folder, then you do not need to mention any path an straight you can name the module. But if your js is in the root then you can access to it thru this address:

(“./getname.js”) // Name can be anything based on your module name

“Require” creates an object from the module, so after that you can access all exported function via that object:


testmodule.getname('Mehran');
testmodule.Printname();

I hope this example helps you to understand Node module better, Node.js is like a puzzle you need to spend time to understand the whole story, and it may not be easy as the environment and concept is a bit different with what we used to it.

you can download full set of code form my github :

https://github.com/CodingTips/tutorial

for any questions you can contact me through my email :
janfeshan.mehran@gamil.com

bye bye

How can connect to the MongoDB through Node.js?

Hey guys,
Today I am going to show you how connect to MongoDB through Node.js. Please follow the below steps:
Note: for those that do not know how to install and use MongoDB please what this Video or ready this post
Note: I am using Webstorm to develop Node.js, you can install 30 days trail version and if you found it is good then buy it or you can use Code IDE which is an IDE that is provided by Microsoft and it is free 
If you use Webstorm, it creates everything for you and you only need to change the code and no need to be worry about the packages and other settings that normally needed for Node.js, here is my Webstorm screen:

There are different libraries that can help you to connect to MongoDB, for this tutorial I am using monk, which is simple and very light.
1. Add these few lines of code to app.js file :


var mong = require('mongodb'); // we import MongoDB
// library to our project
var monk = require('monk');// we create an object 
//from monk liberay in order to connect to MongoDb
var db = monk('localhost:27017/userlist');// we create an database 
//object which is connected to our ruing MongoDB instance

2. In the next step we need to set db in all requests that come to our server, it is a bit different with what we have done in ASP.net. you must be aware that concept in Nodde.js is very different with what you know in ASP.net or even Java.


app.use(function(req,res,next){
req.db = db;
next();
});

3. This few lines of code must be add before:


app.use('/', routes);
app.use('/users', users);

4. After these two simple steps, our app.js is ready to connect to MongoDB and gave service to rest of application. Now we need to add two functions to our route and create two more views in order to show the result and get data from user. Direct go to the route folder and open index.js and add these two functions:

Get data:


router.get('/userlist',function(req,res){
var db= req.db;
var collection=db.get('testcollection');
collection.find({},{},function(e,docs){

res.render('userlist',{"userlist":docs

});

});

});

Receive post request and insert in database.

router.post('/adduser', function(req, res) {

// Set our internal DB variable
var db = req.db;

// Get our form values. These rely on the "name" attributes
var userName = req.body.username;
var userEmail = req.body.useremail;

// Set our collection
var collection = db.get('testcollection');

// Submit to the DB
collection.insert({
"name" : userName,
"email" : userEmail
}, function (err, doc) {
if (err) {
// If it failed, return error
res.send("There was a problem adding the information to the database.");
}
else {
// And forward to success page
res.redirect("userlist");
}
});
});

We need two different views for these two functions, To add view, straight go to the view folder, copy and paste index.jade and change name to userlist ‘and do the same and name it adduser.

Open userlist and copy this code there:

extends layout

block content
h1.
User List
ul
each user, i in userlist
li
a(href="mailto:#{user.email}")= user.name

Open adduser and copy this code there:

extends layout
block content
h1= title
form#formAddUser(name="adduser",method="post",action="/adduser")
input#inputUserName(type="text", placeholder="username", name="username")
input#inputUserEmail(type="text", placeholder="useremail", name="useremail")
button#btnSubmit(type="submit") submit

I tried to explain everything in a short way. I will try to upload and video to show you everything.

Run your project and go to these two address to see your result:

http://localhot:3000/userlist

http://localhot:3000/adduser

Bye bye

How can Install MongoDB and Create Database?

Hey Guys,

Now it is time to do some fantastic thing, normally connecting to database and retrieving data from database or writing into database is quite interesting if you are working with new platform and tools. Today I want to show you how to connect to the MongoDB and retrieve data (of course I will teach you how to insert first), for a few seconds forget about node.js and focus on MongoDB, in order to create a database in MongoDB we need to follow the below steps:

  1. MongoDB is an open source database which can be downloaded from the: http://www.mongodb.org . So please open the link and download it. For this tutorial we need MongoDB version Windows 64-bit 2008 R2+
  2. If you cannot do it please close the website and turn off your pc and do something else as you are not qualified for developing. (Just kidding) if you have difficulty to download or install, please click here to see the video which I uploaded on my YouTube channel.
  3. After you install, you need to run your MongoDB server, it is very easy.
    1. Please make sure that you created Data folder in your node folder, I created something like this : E:\node\nodetest1\data
    2. Now we want to create database and copy necessarily files into the data folder. Open command prompt and go to the MongoDB bin folder, (if you watch video you can see how I moved to bin folder). For those lazy people that do not want watch video you can find Bin folder in this address:

C:\MongoDB\Server\3.0\bin

After you moved to this folder key in the below command:

mongod –dbpath E:\node\notetest1\data

Note:  dbpath is the address of data folder, so maybe your data folder address is different from mine, please change it accordingly.

Note: In some cases I saw that mongod does not work, so for this case you need to key in the full address of mongod.exe, actually mongod is exe file in bin folder!!!!

Note: it may take a few seconds to a few minutes for command to be finished so please be patient.

  1. After you ran the command you could see a few files and folders are created in Data folder and also in your command prompt window you can see MongoDB is waiting for connection.
  2. It means you are in a right direction and your MongoDB is waiting for connection.
  3. Something like this:
  4. Be default mongoDB is connected to the test database and even you can see the name of test on your screen once you copy all the necessarily  files to the data folder, but do not worry about it, we can always switch to our own database, for this purpose you need to run the MondoShell, to run MondoShell you need to run the below command:

C:\MongoDB\Bin\mongo.exe

After you press enter you can see something like this:

MongoDB Wating for connection

To change the database you can type this command:

Use [name of you database]

Connect to Differtent Database

Mongo DB uses Json data structure to keep data, so before we go further I want to show you how Json format looks like:

{“name”:”Mehran”,”code”:”123”}

{“name”:”Arash”,”code”:”234VF”}

{“name”:”Arman”,”code”:”EDRng”}

Basically json format is very simple and understandable for human.

How can we add record to our database?

In order to add records to your database you can use the below command:

db.insert.testcollection.insert({“name”:”Arman”,”code”:”EDRng”})

In the above command, testcollection is a user defined name for the collection. It means you can change it as you want. This collection looks like and array and keep all the records. Above command must be ran in mongo shell. (Same as step 5).

How can we retrieve records?

In order to retrieve data you can run the below command:

db.testcollection.find ().pretty ()

Then result is something like this:

{

“_id” : ObjectId(“55d1b2028f5be420d8a167fc”),

“name” : “Mehran”,

“code” : “123”

}

MongoDB use

in the next post I will teach you how to retrieve data from MongoDb and show it Html file by using Node.js.

bye bye 🙂

Why Node.Js? install express,Mongo DB and Jade

Hey Guys,

In this tutorial I want to show you how we can convert Node.Js to a web-server or even more. To do this conversion you need to install express, and also you need to do a few things:

run you command prompt and follow the steps:

  1. Create a directory for Node.js project

E:/mkdir node

2. now you need to install express, please use this command:

E:\node>npm install -g express-generator

3. Create a simple project in your node directly that you created in step 1

E:\node>express nodetest1

4. if you go to that folder you can see a few files and folders are added, this is simple project which is created for us and we want o change some of the configurations. if you open the node folder you can see the package.json, this json file contains list of dependency packages that are needed for this project. if you open the file in any text editor you can see something like :

{
“name”: “nodetest1”,
“version”: “0.0.0”,
“private”: true,
“scripts”: {
“start”: “node ./bin/www”
},
“dependencies”: {
“body-parser”: “~1.12.4”,
“cookie-parser”: “~1.3.5”,
“debug”: “~2.2.0”,
“express”: “~4.12.4”,
“jade”: “~1.9.2”,
“morgan”: “~1.5.3”,
“serve-favicon”: “~2.2.1”
}
}

we only need to add a few items to the dependencies, as I want to show you how to use mongoDB in node js, so we need to add mongoDB dependency to this file. Please change the dependencies section like this:

“dependencies”: {
“body-parser”: “~1.12.4”,
“cookie-parser”: “~1.3.5”,
“debug”: “~2.2.0”,
“express”: “~4.12.4”,
“jade”: “~1.9.2”,
“morgan”: “~1.5.3”,
“serve-favicon”: “~2.2.1”,
“mongodb”: “~2.0.33”,
“monk”: “~1.0.1”
}

5. Next step is to install all dependencies that are mentioned in the package.json. run the command prompt and execute the below command:

E:\node\nodetest1>npm install

6.Once you install everything, you need to create a data directity in node folder, the reason that we need to create data folder is that we want to user mongoDB and we already installed dependency for mongoDB, so without data folder you will face with error 🙂 , so easily run this command in command prompt:

E:\node\nodetest1>mkdir data

7. Now everything is ready to run the Node.js, to run Node.js  you can execute the below command:

 E:\node\nodetest1>npm start

8: To see the result of what have done in this tutorial type this address in browser:

http://localhost:3000

Maybe you are still a bit confused about node.js, but do not be please, it is very normal specially for those programmers that are not familiar with these kind of coding and environments. we just started and you need go through the whole training to understand what exactly is happening. 🙂

Bye bye

Why Node.js?

Hey Guys,

I decided to start a tutorial about node.js. I know sounds is boring because there are plenty tutorials outside which can help you to learn. but in this tutorial I want to start differently. I want to start developing a real application. but we start it slowly and I will try to post  a few times per day. before we start lets say why node.js ?

My answer to this questions is : because node.js is very cool. hahaha… but if you really want to know why node.js,  you can look at this web page, this is a really good explanation about node.js

Why node.js?

Any way, after you read Why Node.js, please do not waste your time to search about it. if you really want to know more about it, you only have one choice:

  1. Move your ass and work on it … hahahah

How we can install Node.Js?

Ok, this a first question that you may ask and it is right question. to install node.Js, Please click Node.Js  and click on the Green INSTALL button ( I hope you can see it there !!!!! 😉  ). Just follow the install instruction and install the Node.Js,

Note: if you do not know how to install this app please turn off your PC and do something else. because installing node.js is as easy as installing Adobe Acrobat Reader 5.5.

How can check whether the Node.Js is installed properly or not?

it is a good questions. to check Node.Js installed properly or not, please open command prompt and type node -v

In response you may get something like this :

Node Version Control

I think for now is enough, I will try to post new things :

Bye bye