The only real dev language - sahajss/knowledge_base GitHub Wiki

Node.js

###Why Node.js?

  1. It scales really well if its run on a powerful server.
  2. I already know javascript, so I don't have to translate anything into another language.
  3. It's really fast, when computing a lot of data it is noticeably faster than other frameworks.
  4. Express.js is really simple to use, and has many modules you can install to solve any problem.
  5. It is good to know for the future. Many companies are looking to hire developers who know Node.js. Also, it is not going to die. It is rather young and can only grow.
  6. It's "hip". It's the only real dev language.

###Things to know about Node.js First of all it is based on callbacks. Once you start getting into more intense problems all the callbacks start to mess with your head. It's like super recursion. <----- eww right? Another thing interesting with Node.js, is the prominent use of middleware. Middleware is basically any packages you have to install. This middleware helps you solve many prominent problems you might face when building an application, like logins and bootstrap. You can also make your own middleware by using modules. Modules are like classes, in a way. You can use a module to hold a function that you will use to solve a certain class. The following module and program code below is an example of how to use modules. Here the "initial program" is getting the arguments from the terminal which is a file located in the directory. The "initial program" calls a method from the module which seperates the name of the file and the extension and returns it to the "initial program". This snippet of code is also a good example of callbacks. It uses a callback to send back the list to the "initial program".

"Initial Program":

var mymodule = require('./module');
mymodule(process.argv[2], process.argv[3], function(err, results) {
  if (err) {
    console.log('Error: ', err);
  }
  
  results.forEach(function(item) {
    console.log(item)
  });
});

The module:

module.exports = function(dir, ext, cb) {
	var fs = require('fs'),
		ext = '.' + ext;

	fs.readdir(dir, function(err, list) {
		if(err) {
			return cb(err);
		}

		var results = [];
		list.forEach(function(item) {
			if (item.indexOf(ext) > -1) {
				results.push(item);
			}
		});

		cb(null, results);
	});
};

###Express.js??? Node.js is not a javascript framework. It's considerd a runtime environment. Which means its basically another programming language like Python or Java. However, you can use Javascript frameworks with it to make it do amazing thins. One of the most simple Javascript frameworks available is Express.js, and frameworks like this are what allow Node.js to connect to ports and develop server-side applications. Express.js, in theory, is another module that you can access with the following little snippet:

var express = require('express');

The following is a little express.js app that allows node.js to connect to a port specified in an argument. And, if the user were to go to /home they would be greeted with "hello world".

var express = require('express')
var app = express()
app.get('/home', function(req, res) {
  res.end('Hello World!')
})
app.listen(process.argv[2])

###How can I install this voodoo?? Great question, I am glad you are interested. You want to go to this website, https://nodejs.org/en/, and install stable version of Node.js. This also installs the package manager, NPM, which will also be of use. After you have this installed you are going to want to make your first application. First you are going to want to install a tool called express generator, which basically creates a scaffolding for you. $ npm install express-generator -g After you install this you can make your app. express yourappname This will make the scaffolding and everything, but before you do anything you are going to want to install express in the directory.

cd yourappname
install express

Then you can run your app and access it on localhost with the following command.

npm start

After this node.js and express.js should be up and running!!!