why client and server separate folders in yo generator web apps

Supported Configurations

Client

  • Scripts: JavaScript, CoffeeScript, Babel
  • Markup: HTML, Jade
  • Stylesheets: CSS, Stylus, Sass, Less,
  • Angular Routers: ngRoute, ui-router

Server

  • Database: None, MongoDB
  • Authentication boilerplate: Yes, No
  • oAuth integrations: Facebook Twitter Google
  • Socket.io integration: Yes, No
Advertisements

when we should go for nodejs

To Say Generic,

  • is a command-line tool that can be run as a regular web server and lets one run JavaScript programs
  • utilizes the great V8 JavaScript engine
  • is very good when you need to do several things at the same time
  • is event-based so all the wonderful Ajax-like stuff can be done on the server side
  • lets us share code between the browser and the backend

Node.js is especially suited for applications where you’d like to maintain a persistent connection from the browser back to the server. Using a technique known as “long-polling”, you can write an application that sends updates to the user in real time. Doing long polling on many of the web’s giants, like Ruby on Rails or Django, would create immense load on the server, because each active client eats up one server process. This situation amounts to a tarpit attack. When you use something like Node.js, the server has no need of maintaining separate threads for each open connection.

This means you can create a browser-based chat application in Node.js that takes almost no system resources to serve a great many clients. Any time you want to do this sort of long-polling, Node.js is a great option.

Why cant we use old traditional methods in sending data like ajax,push etc etc

Sending Data

Nowadays, to send a request to a server, it is very common to use AJAX to send an XMLHTTPRequest to the server without any page reloads.

Polling

One way to solve this issue is by requesting the server periodically. This could be achieved by a simple setInterval function with an ajax call to the server every x seconds. This would be called polling, because we are constantly asking the server, ‘do you have anything new?’.

Push Technology – Event Based Programming

But what if the server wants to send the client new data that the client is interested in, but has no idea when the data would be available? This is sometimes referred to as push technology, where a server wants to send data to a client. A way to think about push technology is to only do something when there’s a reason. That is, the server will only send a data response when there’s data available.

Long Polling

Long polling is where the client requests new data from the server, but the server does not respond until there is data. In the meantime, the client has an open connection to the server and is able to accept new data once the server has it ready to send.

subscribe: function(callback) {
      var longPoll = function(){
          $.ajax({
              method: 'GET',
              url: '/messages', 
              success: function(data){
                  callback(data)
              },
              complete: function(){
                  longPoll()
              },
              timeout: 30000
          })
      }
      longPoll()
  }

When it comes to Nodejs:

On the server side, the request is then subscribed to the message bus.
In Nodejs, the message bus is built in and can be required as follows. And you’ll probably want to set max listeners to more than the default of 10.

var EventEmitter = require('events').EventEmitter  
var messageBus = new EventEmitter()  
messageBus.setMaxListeners(100)  

Subscring the request.

router.get('/messages', function(req, res){  
    var addMessageListener = function(res){
        messageBus.once('message', function(data){
            res.json(data)
        })
    }
    addMessageListener(res)
})

And so you’re wondering, when will this message bus ever fire back data to the response?
Since we have a subscriber to the event, we also need a publisher to cause an event.

To emit a message to the message bus, the client, or something else, will need to initiate a publish event.

publish: function(data) {  
    $.post('/messages', data)
}

And so the server can then emit a message to the message bus.

router.post('/messages', function(req, res){  
    messageBus.emit('message', req.body)
    res.status(200).end()
})

In this set up, you ensure that every client continually has an open connection to the server, but the server is not easily overloaded to a multitude of requests, because it puts every request/response inside a message bus to be emitted once it is ready.
This method has been widely popular due to its relatively easy set up and wide support accross all browsers.

 How does it work??
toptal-blog-1_B

A quick calculation: assuming that each thread potentially has an accompanying 2 MB of memory with it, running on a system with 8 GB of RAM puts us at a theoretical maximum of 4000 concurrent connections, plus the cost of context-switching between threads. That’s the scenario you typically deal with in traditional web-serving techniques. By avoiding all that, Node.js achieves scalability levels of over 1M concurrent connections (as a proof-of-concept).

There is, of course, the question of sharing a single thread between all clients requests, and it is a potential pitfall of writing Node.js applications. Firstly, heavy computation could choke up Node’s single thread and cause problems for all clients (more on this later) as incoming requests would be blocked until said computation was completed. Secondly, developers need to be really careful not to allow an exception bubbling up to the core (topmost) Node.js event loop, which will cause the Node.js instance to terminate (effectively crashing the program).

The technique used to avoid exceptions bubbling up to the surface is passing errors back to the caller as callback parameters (instead of throwing them, like in other environments). Even if some unhandled exception manages to bubble up, there are mutiple paradigms and tools available to monitor the Node process and perform the necessary recovery of a crashed instance (although you won’t be able to recover users’ sessions), the most common being the Forever module, or a different approach with external system toolsupstart and monit.

NPM: The Node Package Manager

When discussing Node.js, one thing that definitely should not be omitted is built-in support for package management using the NPM tool that comes by default with every Node.js installation. The idea of NPM modules is quite similar to that of Ruby Gems: a set of publicly available, reusable components, available through easy installation via an online repository, with version and dependency management.

A full list of packaged modules can be found on the NPM website https://npmjs.org/ , or accessed using the NPM CLI tool that automatically gets installed with Node.js.



How to create a static server using nodejs

Folks,

Good Noon..

Good things happens with good sharing of knowledge..

I gain something good knowledge from this great techie geeky Ryan Florence today.

Actually my scenario looks like this..

I have to build a project say project zero..where it have to run on its own without any wamp/IIS servers. Project should run with its own server using nodejs. So i have a filename called app.js where that single js file acts like a server to run my index.html page in http://localhost:8888

What i have to do for all this is only two steps:

1. run the command in command prompt (myprojectpath>node app.js)

2. hit the url in my funky browser. (http://localhost:8888)

Lets go through the code now..

app.js:

var http = require(“http”),
url = require(“url”),
path = require(“path”),
fs = require(“fs”)
port = process.argv[2] || 8888;

http.createServer(function(request, response) {

var uri = url.parse(request.url).pathname
, filename = path.join(process.cwd(), uri);

path.exists(filename, function(exists) {
if(!exists) {
response.writeHead(404, {“Content-Type”: “text/plain”});
response.write(“404 Not Found\n”);
response.end();
return;
}

if (fs.statSync(filename).isDirectory()) filename += ‘/index.html’;

fs.readFile(filename, “binary”, function(err, file) {
if(err) {
response.writeHead(500, {“Content-Type”: “text/plain”});
response.write(err + “\n”);
response.end();
return;
}

response.writeHead(200);
response.write(file, “binary”);
response.end();
});
});
}).listen(parseInt(port, 10));

console.log(“Static file server running at\n => http://localhost:” + port + “/\nCTRL + C to shutdown”);

index.html:

This is index.html

Steps to follow to run and check:

1. Open command prompt and do as follows

14-12-2014 13-30-01

2. Just type http:localhost:8888

14-12-2014 13-32-30

Thats it..soo crazy code right 😉

Just say thanks to https://gist.github.com/rpflorence 🙂

Source: https://gist.github.com/rpflorence/701407

 

how to install node_modules/expressjs to specific directory

Folks,

In general after installation of nodejs if we type npm install, it install all necessary modules-this is quite known to all..

if you want to install specifically express like that..we will command node with npm install -g express which installs express-this is also known.

But if at all we required to install node_modules/express js to our project directory means??

very simple..navigate to your directory in cmd prompt like D:/workspace/your_project and type npm install -l express like shown below..

21-07-2014 15-00-57

 

thats it..now you can see a folder named node_modules in your directory..

21-07-2014 15-03-07

The concept here is global vs local

21-07-2014 15-04-16

Installation of mongodb in windows

Lets wrench the installation of mongodb today.

Note: Here in this tutorial we are using in different way like how mysql stores all projects in bin of mysql. We are doing the same by creating a common workspace where all the databases store in data folder of mongodb folder. You can maintain a separate db space in your project folder by following this previous tutorial

First download zip file(preferably) or exe file from mongodb

Once after install or extract of mongodb you will find the following files like shown below

29-05-2014 12-22-59

Now copy those files and create a directory in c drive and make sure the path should look like

c:/mongodb

Here paste the files. Before we startup some thing lets create a couple of directories name “data” and “log” in mongodb fodler. So finally it should look like

29-05-2014 12-25-43

Inside of data folder again create a folder name “db” //all database data will store here

Now install git in your machine in replacement of command prompt.

Hope the installation is complete now…lets step forward

goto c:/mongodb/ folder. right click on bin folder and click “Git Bash” as shown below

29-05-2014 12-34-19

Hope you can see gitbash prompt which resembles like cmd

Now we should define default directory,db path and log manually with commands as shown below

Note: ‘/’ or ‘\’ is very important here in command prompts coz unix style use ‘\’ which works in most cases.

29-05-2014 12-40-58

From the above image you can see the command we should type and hit enter to set default directory,log path, log appends(instead of overwriting in mongodb.log everytime) and to use rest services.

Now you can see log name “mongodb.log” as shown below

29-05-2014 12-43-52

Now go back to mongodb folder and right click bin folder again and click gitbash

Type mongo and hit enter. you can see its connected

29-05-2014 12-46-37

Thats it now you can check dbs by show dbs command and create collections and documents as you wish.

Now we need install mongodb as a window service else you will be figured out with an error showing connection failed.

For that you just need to append –install command to the above command where the command finally will be

> mongod –directoryperdb –dbpath c:/mongodb/data/db  –logpath c:/mongodb/log/mongodb.log –logappend –rest –install

Hit enter, with that it will create a service for us.

29-05-2014 12-55-18

Just type “net start mongoDB

29-05-2014 12-58-10

To verify whether mongodb started or not..click windows and type “services.msc” where you will find a wrench gear up icon..click on it and check whether mongodb is started or not.

With this we completed successfully installing and starting mongodb. Lets goto c:/mongodb/bin gitbash prompt and type “mongo”.

Show databases:

Now you can see databases by showdbs command

Create/switch to database:

create db by use database_name command (eg: use sathya will create a database and switches to that database).

Show collections:

show collections – will show the list of collections(tables in oracle)

db.createCollection(“collectionname”) – will create a collection in your database

Lets take an example to insert data in collections..Now i will create a table called employee and stores(create/update/delete) some records in it.

Create collection:

Eg: db.createCollection(“employee”);

the above command creates a employee collection(table) in db. Now lets insert some stuff in it.

Insert records:

db.employee.insert({name:”sathya”,dept:”computers”,technology:”html5″}) – hit enter

To check the record inserted or not..use db.employee.find() – this results an unorder/unformatted content. Lets put up some make up for this.. 😉

Find records:

use command – db.employee.find().pretty()

Find Only one field:

If you wish to find only one employee with particular name..

just type db.employee.findOne({name:”sathya”}) – this results only one employee with name sathya

Update command:

db.employee.update({oldname},{new name},{ upsert})

Eg: db.employee.update({name:”sathya”}.{name:”SathyaManoj”}.{upsert:true})

You can even modify different fields/columns by db.employee.update({name:”sathya”,dept:”computers”}.{name:”SathyaManoj”,dept:”IT”}.{upsert:true})

This will update the name sathya with SathyaManoj. And what this upsert does is..it will check for the name sathya to update, if it doesnt find it..then it will create a new one.

Now check it by using db.employee.find().pretty()

Delete command:

db.employee.remove({name:”sathya”}) – will remove the record from the collection.

What is mongodb? Basics of mongodb

MongoDB is a cross-platform, document oriented database that provides, high performance, high availability, and easy scalability. MongoDB works on concept of collection and document.

Database

Database is a physical container for collections. Each database gets its own set of files on the file system. A single MongoDB server typically has multiple databases.

Collection

Collection is a group of MongoDB documents. It is the equivalent of an RDBMS table. A collection exists within a single database. Collections do not enforce a schema. Documents within a collection can have different fields. Typically, all documents in a collection are of similar or related purpose.

Document

A document is a set of key-value pairs. Documents have dynamic schema. Dynamic schema means that documents in the same collection do not need to have the same set of fields or structure, and common fields in a collection’s documents may hold different types of data.

Below given table shows the relationship of RDBMS terminology with MongoDB

RDBMS MongoDB
Database Database
Table Collection
Tuple/Row Document
column Field
Table Join Embedded Documents
Primary Key Primary Key (Default key _id provided by mongodb itself)
Database Server and Client
Mysqld/Oracle mongod
mysql/sqlplus mongo

 

Sample document

Below given example shows the document structure of a blog site which is simply a comma separated key value pair.

{
   _id:ObjectId(7df78ad8902c)
   title:'MongoDB Overview', 
   description:'MongoDB is no sql database',by:'tutorials point',
   url:'http://www.tutorialspoint.com',
   tags:['mongodb','database','NoSQL'],
   likes:100, 
   comments:[{
         user:'user1',
         message:'My first comment',
         dateCreated:newDate(2011,1,20,2,15),
         like:0},{
         user:'user2',
         message:'My second comments',
         dateCreated:newDate(2011,1,25,7,45),
         like:5}]}

_id is a 12 bytes hexadecimal number which assures the uniqueness of every document. You can provide _id while inserting the document. If you didn’t provide then MongoDB provide a unique id for every document. These 12 bytes first 4 bytes for the current timestamp, next 3 bytes for machine id, next 2 bytes for process id of mongodb server and remaining 3 bytes are simple incremental value.

Any relational database has a typical schema design that shows number of tables and the relationship between these tables. While in MongoDB there is no concept of relationship

Advantages of MongoDB over RDBMS

  • Schema less : MongoDB is document database in which one collection holds different different documents. Number of fields, content and size of the document can be differ from one document to another.
  • Structure of a single object is clear
  • No complex joins
  • Deep query-ability. MongoDB supports dynamic queries on documents using a document-based query language that’s nearly as powerful as SQL
  • Tuning
  • Ease of scale-out: MongoDB is easy to scale
  • Conversion / mapping of application objects to database objects not needed
  • Uses internal memory for storing the (windowed) working set, enabling faster access of data

Why should use MongoDB

  • Document Oriented Storage : Data is stored in the form of JSON style documents
  • Index on any attribute
  • Replication & High Availability
  • Auto-Sharding
  • Rich Queries
  • Fast In-Place Updates
  • Professional Support By MongoDB

Where should use MongoDB?

  • Big Data
  • Content Management and Delivery
  • Mobile and Social Infrastructure
  • User Data Management
  • Data Hub

     

 External References:

http://www.tutorialspoint.com/mongodb/mongodb_tutorial.pdf

http://try.mongodb.org/

http://mongodb.github.io/node-mongodb-native/api-articles/nodekoarticle1.html

http://blog.ksetyadi.com/2011/10/nodejs-and-mongodb-a-beginners-approach/

http://www.ibm.com/developerworks/library/wa-notify-app/

How to install mongodb and connect to your project part-1

Folks,

Lets continue with my previous post where i created a project named nodetest in d drive. For instance, lets use the same project here to install and use mongodb.

Before stepinto that..lets know briefly what is mongodb

MONGODB

  • NoSQL database
  • Well documented, huge community
  • Databases = Databases
  • Collections = Tables
  • Documents = Rows
  • Nested documents

For more on documentation of mongodb clickhere

There are many libraries like mongodb,mongoose etc in market. To know the major difference between mongoose and mongodb? clickhere

First download exe file from mongodb.org

Now install it in your machine. Here my path of mongo db installation is C:\Program Files\MongoDB 2.6 Standard

Lets move to next step

RUN MONGOD AND MONGO

In your nodetest directory, create a subdir called “data”. Then navigate to the directory in which you placed your MongoDB files (let’s say C:\Program Files\MongoDB 2.6 Standard\bin). From that directory, type the following:

COMMAND C:\MONGO\
mongod --dbpath c:\node\nodetest1\data

28-05-2014 16-22-23

You’ll see the Mongo server start up. This is going to take a while if it’s the first time, because it has to do some preallocating of space and a few other housekeeping tasks. Once it says “[initandlisten] waiting for connections on port 27017”, you’re good. There’s nothing more to do here; the server is running. Now you need to open a second command prompt. Navigate again to your Mongo installation directory, and type:

COMMAND C:\Program Files\MongoDB 2.6 Standard\bin
mongo

You’ll see something like the following:

MONGO CONSOLE
c:\mongo>mongo
MongoDB shell version:2.4.5
connecting to: test

28-05-2014 16-23-53

 

Don’t worry about “connecting to: test” … that’s just the default database Mongo decides to use if you don’t specify one on the command line, which we didn’t because it’s not important right now. It doesn’t actually even create the “test” database unless you add a record. It’d be totally fine to just work in that database for right now, but let’s make one of our own. In your Mongo console, type the following:

MONGO CONSOLE
use nodetest

28-05-2014 16-44-14
From the above shown image you can see, Switched to db nodetest after you hit enter.

To make the database exist, we have to add some data. We’re going to start off by doing that right inside of the Mongo client.

 ADD SOME DATA

Let’s add a record to our collection. For the purposes of this tutorial, we’re just going to have a simple database of usernames and email addresses. 

In your Mongo client, type this:

MONGO CONSOLE
db.usercollection.insert({"username":"sathya","email":"sathya@log.com"})

28-05-2014 16-49-35

You can see inserted as a result once you hit enter

Something important to note here: that “db” stands for our database, which as mentioned above we’ve defined as “nodetest1”. The “usercollection” part is our collection. Note that there wasn’t a step where we created the “usercollection” collection. That’s because the first time we add to it, it’s going to be auto-created.

Type this:

MONGO CONSOLE
db.usercollection.find().pretty()

In case you’re curious, the .pretty() method gives us linebreaks. It will return as shown below..

28-05-2014 16-53-03

let’s add a a couple more. In your Mongo console, type the following:

MONGO CONSOLE
newstuff =[{"username":"abc","email":"abc@domain.com"},{"username":"xyz","email":"xyz@domain.com"}]
db.usercollection.insert(newstuff);

Note that, yes, we can pass an array with multiple objects to our collection. Handy! Another use of db.usercollection.find().pretty() will show all three records.

Reference: http://cwbuecheler.com/web/tutorials/2013/node-express-mongo/

In above process, we gave db path for the application folder where db itself maintained in project folder. Else you can try in different way like how mysql stores all projects in bin of mysql. You can achieve the same by the following video. Give a try.

Download git from here