Transcript
Page 1: Complete MVC on NodeJS

Complete MVC with NodeJS

Hüseyin BABAL

Software Developer, CSM

@Sony Eurasia

Page 2: Complete MVC on NodeJS

Who Am I Hüseyin BABAL

Software Developer, CSM

@Sony Eurasia

Open Source Comitter

Linux, JAVA, PHP, MongoDB, Javascript, NodeJS, Elasticsearch are my

job

GDG event speaker

Page 3: Complete MVC on NodeJS

WHAT IS NODE.JS?

Page 4: Complete MVC on NodeJS

NodeJS is a software platform that is

used to build scalable network

applications

Page 5: Complete MVC on NodeJS

● Author: Ryan Lienhart Dahl

● Built on Chrome V8 Engine

● Written in C++, Javascript

● Sponsored by Joyent

Page 6: Complete MVC on NodeJS

SO, WHAT IS NODE.JS?

Page 7: Complete MVC on NodeJS

V8 Engine

JS on Server Side

Single Thread

Event Loop

Non-Blocking I/O

Page 8: Complete MVC on NodeJS

WHY TO USE

NODE.JS?

Page 9: Complete MVC on NodeJS

I/O is Expensive

Page 10: Complete MVC on NodeJS

Apache:Memory

usage increases

per connection

Nginx:Constant

memory usage

per connection,

because nginx

uses event loop

mechanism

Page 11: Complete MVC on NodeJS

Dealing with I/O Problems

● Synchronous - One request at a time

● Fork - New process for each request

● Thread - New thread for each request

Page 12: Complete MVC on NodeJS

Thread per request is memory expensive!

What if all threads are busy?

Multithread code is;

● difficult to debug

● difficult to write

● in efficiently performed

Page 13: Complete MVC on NodeJS

How about this answer?

Single thread with non-blocking I/O

Page 14: Complete MVC on NodeJS

WHAT IS EVENT LOOP

AND NON-BLOCKING

I/O?

Page 15: Complete MVC on NodeJS

Do the job and

notify program when

it’s done

Block process until

job is done

Busy

process

Process

available

cb

Blocking I/O

Non-Blocking I/O

Event Stack

Page 16: Complete MVC on NodeJS

Assign jobs to event stack, you will be

notified

One single thread can handle entire

program

Traditional threaded architecture is not

an easy thing

More thread means more process,

equals more memory

Min cost, speed development

Scalable architecture

Callback functions

Page 17: Complete MVC on NodeJS

SEEMS COOL, HOW

ABOUT CODING?

Page 18: Complete MVC on NodeJS

You know JS? Then you

code

NPM (Node Package

Manager)

Available

Windows/Linux/MAC

Webstorm is the best,

also use notepad++

Page 19: Complete MVC on NodeJS

npm install “module_name” Import module with “require”

Simple web server!!!

File : server.js

node server.js

Page 20: Complete MVC on NodeJS

To test web server, go to url http://127.0.0.1:1337 What about if you try to make a system have such url: http://127.0.0.1:1337/product/1234.html ???

Page 21: Complete MVC on NodeJS

ANSWER:EXPRESS.JS

Page 22: Complete MVC on NodeJS

Sinatra inspired web development

framework for node.js

Page 23: Complete MVC on NodeJS

HOWTO START?

Page 24: Complete MVC on NodeJS

You have already installed node.js before…

npm install express -g

That’s it! You can use express framework on your

project

Page 25: Complete MVC on NodeJS

Ideal project structure for nodejs

based web applications

Page 26: Complete MVC on NodeJS

View

{jade}

Controller

{express

built-in fns}

Model

{mongoose}

Page 27: Complete MVC on NodeJS

A quick scenario…

● User goes http://127.0.0.1:1337/tickets/devfestticket-

54332.html

● System fetchs ticket information from db by using

id(54332)

● Assign ticket information to view

● Render view and send to user browser

Page 28: Complete MVC on NodeJS

MODEL DESIGNING

Page 29: Complete MVC on NodeJS

Ticket = new Schema({

tId:{

type:String,

required:true

},

title:{

type:String,

required:true

},

organizationDate:{

type:Date,

required:true

},

owner:{

type:String,

validate:[validator({

length:{

min:3,

max:20

}

}), "username"],

required:false,

default:"anonymous"

}

});

Yes, model definition in json format. Actually, all

the data in nodejs world are in json format.

Model definition is done. This model should be

exists under “models” folder.

Require this model when you need to use

Assume this model defined in a file called

“Ticket.js”

Page 30: Complete MVC on NodeJS

CONTROLLER

Page 31: Complete MVC on NodeJS

*Controller isthe behaviour of your program

*Put your controller definition files under “controller” folder.

Page 32: Complete MVC on NodeJS

VIEW

Page 33: Complete MVC on NodeJS

View files are under “views” folder.

You can use template engines with express.js, in this scenario, “jade” used

Create ticket-detail.jade, put it under “views” folder

Page 34: Complete MVC on NodeJS

Indendation based template engine

Page 35: Complete MVC on NodeJS

COMBINE ALL

Page 36: Complete MVC on NodeJS

Controllers,

Views, Models

managed by main

bootstrap file

called “app.js”

Go to project

folder and say

node app.js

1

2

3

4

5

Page 37: Complete MVC on NodeJS

BEST PRACTICES

Page 38: Complete MVC on NodeJS
Page 39: Complete MVC on NodeJS
Page 40: Complete MVC on NodeJS
Page 41: Complete MVC on NodeJS
Page 42: Complete MVC on NodeJS

WHO USES NODEJS?

Page 43: Complete MVC on NodeJS
Page 44: Complete MVC on NodeJS

Node.js https://github.com/joyent/node

express.js https://github.com/visionmedia/express

mongoose https://github.com/LearnBoost/mongoose

jade https://github.com/visionmedia/jade

Express Simple Blog https://github.com/nodejstr/expressSimpleBlog

NodeJS Türkiye http://www.nodejstr.com/

Page 45: Complete MVC on NodeJS

Questions?

Page 46: Complete MVC on NodeJS

Thank you


Top Related