Getting Started With NodeJS

NodeJs allow you to write server side javascript. If you've been a webdevelopper with javascript  and javascript libraries like Jquery, and working mostly in classic web agencies, this short article will get you throught the crucial first steps to installing and running a nodeJS application.

 

NodeJS is a program based on the Google Chrome web browser embarked javascript Engine. Until now, you mostly see javascript on browser and not on desktop application for example.

Some smart folk had the idea to use the javascript engine of Google Chrome ( called V8) as a standalone program to run javascript.

Now you can use Node to run a webserver (Yes it replaces Apache or IIS), or to run javascript programs on Windows or Linux. Github.com has written an IDE that runs on nodeJS. It is called Atom. More and more website are run on nodeJS server, it has a smaller memory usage than Apache, so it is very lightweight.

In the field of Internet of Things, we start to see people run Iot device in javascript where we traditionally use C language program.

But enough said, i am going to show you how to install NodeJs and start to run scripts.

 

 

Then nodeJs comes with a package manager npm which the central component for writing your server side javascript project.

NodeJs by itself is a small program, and the folks that maintain it use a modular approach to loading libraries.

There are so many libraries you just can't download on your computer. Pne more reason this approach is chosen is because the libraries are rarely frozen, there are regular updates. So this modular approcah will ensure you have the latest version of a library any time, and you don't have to download them manually, npm does it for you with a simple command.

Install nodeJS

First you must install the nodeJs program, you can download it on the official website.

After you installed nodeJS, you are ready to write your firs serverside javascript !

Create a folder, and a file test.js and put inside console.log('Hello World');

Save it and open a terminal, go to the folder and type:

$  node test.js

You'll see :

Hello World!

That's it ! you've written your first nodejs file !

Now let's go a step further, nodeJS is most reknowned for being a webserver. In our cas we want to run nodejs and set it to listen to http request.

 

Your first local node server

 Rename your test.js to server.js

put this code inside

var http =require('http');
var fs =require('fs');
var url =require('url');
// Create a server

http
.createServer(function(request, response){
// Parse the request containing file name

var
pathname = url.parse(request.url).pathname;
// Print the name of the file for which request is made.
console
.log("Request for "+ pathname +" received.");
// Read the requested file content from file system

fs
.readFile(pathname.substr(1),function(err, data){if(err){ console.log(err);
// HTTP Status: 404 : NOT FOUND
// Content Type: text/plain
response
.writeHead(404,{'Content-Type':'text/html'});}else{
//Page found
// HTTP Status: 200 : OK

// Content Type: text/plain

response
.writeHead(200,{'Content-Type':'text/html'});
// Write the content of the file to response body

response
.write(data.toString());}
// Send the response body

response
.end();});}).listen(3000);
// Console will print the message

console
.log('Server running at http://127.0.0.1:3000/');


Create an index.html file with the hello world string inside the same directory.

Now run the node server again :

$ node server.js

navigate on your browser to 127.0.0.1:3000 and there you go !

 

We've seen that we can create a minimalistic website with nodeJS. But you may have heard of ExpressJS. ExpressJS is a minimalistic framework to perform all thing relative to http.

  1. It has a routing system to map url to page content
  2. It allows a dynamic web page rendering similar to MVC parameterized url
  3. Allows to setup middleware to respond to http request

 In a next chapter i will introduce you to ExpressJS

2015-12-05 17:12:45 0 Commentaires

Dumping Results With PDO

With PDO, there is a native method to dump the result of a query.

$stmt->execute();

$stmt->debugDumpParams();

This can compliment the debug method in Twig itself :

{{ dump(result) }}

 

In Symfony2, you can use Doctrine to dump also:

\Doctrine\Common\Util\Debug::dump($products);

2015-01-27 14:01:57 0 Commentaires

Debugging With Twig

Rendering HTML in twig template with Twig

By default whenever you try to pass an HTML string to Twig, it applies a function like html_entities(),
opening bracket < will be displayed as
&lt; , so if you want to render the HTML you should use the raw filter like this:

{{ html_string| raw }}

Echoing an array

{{  dump(products) }}

 

2015-01-21 00:00:00 0 Commentaires

Improve Your OOP Skills With Symfony

If you've been programming in php for several years, chances are you've come across OOP programming because your application has grown in complexity.

Coding in OOP is a very good move (but not that easy at the beginning). And at the end you'd feel the benefit of code reusing.

What is code reuse?


If you develop more than 2 web applications, you will find code reusing time saving.

There shoudl be common stuff to share like user login, form validation to name a few.

But the difficulty is that you may work in two different environment and you will need to modify the first code in the second application. Unless you are comfortable with high level OOP programming (abstract class, interface, polymorphism, dependency injection, namespace, class autoloading), it is inevitable that you will alter the code.

In fact writing reusable code is not that easy, it needs knowledge of advanced OOP concept.

The best way to start is to download a well known API such as FOSUserBundle and see under the hood. It uses lots of abstraction.

Abstraction is the keyword, this mechanism allows you to write generic methods to be used consistently in different frameworks.

Before you can write reusable code (that you can share as open source), first try to master the key concept of OOP programming such as interface, polypmorphism and design pattern.

Key OOP concept to work on

Here is what i'm working on to master OOP:

Interface and abstract classes

Those two have similarities, abstract classes can only be inherited, but can contain non abstract method, while interfaces are 100% abstract classes. Interface are quite bizarre at first, because they define method ans their signature, but there is no code in the method ! They tell you that using interface can allow you to function in different environment, for example using interface allow you to connect to Mysql as well as PostGrest, or SQLite. Okay, how?

 

Polymorphism

In Java, which is a strongly typed language, (a string is a string, a number is a number and not a string), the polymorphism mechanism allow you to manipulate different type of object.

Design pattern

Design pattern are recipes to solve a problem. For example the singleton design pattern, makes it impossible to instantiate more than once a class. Why? if you don't want to mess up with lots of object and manage memory well then use it. Factory is another one that'll be useful, it is a method of object instantiation without using the constructor of a class, in fact you delegate the instantiation of a class to another class, why? if you one day rename the class or change the class, then you'll only need to do it in the factory class, that will save you time !

2014-12-24 16:12:31 0 Commentaires

Component To Know Well When Learning Symfony

Even though you'll want to code without using a OOP framework I encourage you to first code with a framework, you'll learn a lot about the best pratices, then you'll be comfortable when you want to do thing without one.

 

Here are the essential component to know well when you learn Symfony :

Routing

it is the most important component to know because it is responsible for mapping a url request to a controller, there are different ways to do it, using yaml in the routing.yaml fiel, or annotation within your controller methods, or in XML or even directly in PHP.

Things get complicated when you have parameters in your urls (most of the time)

www.example.com/product/755

a route that you'll use to match this request is

acme_store_view:

path: /view/{id}

defaults: { _controller: AcmeStoreBundle:Default:view}

or in annotation

@Route("/view/{id}", name="acme_store_view")

 

Form component

The Form component is tricky, because behind its simplicity, there are a lot of thing done, and the day you want to customize things, difficulties will arise because this complexity usually is hidden, so take an in depth look at the documentation and test test test!

One difficult thing i met is how to customize the forms fields with the glyphicons of Bootstrap.

2014-12-24 10:12:04 0 Commentaires

Walkthrough The AngularJS Tutorial

I Assume this is for Windows 7

If you have difficulties doing the angulars JS toolset on the tutorial from this page :

https://docs.angularjs.org/tutorial

Then these informations may interest you

First you've got to install Git, the versionning tool, and set the path varaible. This page is helpful.

Once you have set the path you must close all active prompt and restart again otherwise the new path will not be set.

Download nodejs installer on the given path, then run it. After install is complete, go to the angular-phonecat folder to do the following thing:

npm install

It will install the needed packages or dependencies. Toi make this work you must create the npm folder in the AppData such as:

C:\Users\username\AppData\Roaming\npm

you may run explorer as an administratot to do this but not sure it's needed.

Don't worry if you don't succeed, these are only tools, you can do the tutorial with a text editor such as SublimeText which is recommended for writing AngularJS scripts.

�

The rest of the tutorial

2014-10-31 16:10:12 0 Commentaires