complete mvc on nodejs

Post on 06-May-2015

4.816 Views

Category:

Technology

11 Downloads

Preview:

Click to see full reader

DESCRIPTION

Complete MVC on NodeJS by using ExpressJS, Mongoose, Jade with best practises

TRANSCRIPT

Complete MVC with NodeJS

Hüseyin BABAL

Software Developer, CSM

@Sony Eurasia

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

WHAT IS NODE.JS?

NodeJS is a software platform that is

used to build scalable network

applications

● Author: Ryan Lienhart Dahl

● Built on Chrome V8 Engine

● Written in C++, Javascript

● Sponsored by Joyent

SO, WHAT IS NODE.JS?

V8 Engine

JS on Server Side

Single Thread

Event Loop

Non-Blocking I/O

WHY TO USE

NODE.JS?

I/O is Expensive

Apache:Memory

usage increases

per connection

Nginx:Constant

memory usage

per connection,

because nginx

uses event loop

mechanism

Dealing with I/O Problems

● Synchronous - One request at a time

● Fork - New process for each request

● Thread - New thread for each request

Thread per request is memory expensive!

What if all threads are busy?

Multithread code is;

● difficult to debug

● difficult to write

● in efficiently performed

How about this answer?

Single thread with non-blocking I/O

WHAT IS EVENT LOOP

AND NON-BLOCKING

I/O?

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

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

SEEMS COOL, HOW

ABOUT CODING?

You know JS? Then you

code

NPM (Node Package

Manager)

Available

Windows/Linux/MAC

Webstorm is the best,

also use notepad++

npm install “module_name” Import module with “require”

Simple web server!!!

File : server.js

node server.js

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 ???

ANSWER:EXPRESS.JS

Sinatra inspired web development

framework for node.js

HOWTO START?

You have already installed node.js before…

npm install express -g

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

project

Ideal project structure for nodejs

based web applications

View

{jade}

Controller

{express

built-in fns}

Model

{mongoose}

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

MODEL DESIGNING

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”

CONTROLLER

*Controller isthe behaviour of your program

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

VIEW

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

Indendation based template engine

COMBINE ALL

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

BEST PRACTICES

WHO USES 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/

Questions?

Thank you

top related