What is NodeJS and what is MEAN

NodeJS = asynchronous event driven JavaScript runtime, Node is designed to build scalable network applications. In the following "hello world" example, many connections can be handled concurrently. Upon each connection the callback is fired, but if there is no work to be done Node is sleeping

*******It has a built in HTTP server library --- you don't need a separate web server like Apache --- the nodeJS application can do the http listening -----***********************

simple NodeJS application that is an http listener and always returns Hello World

const http = require('http');
const hostname = '127.0.0.1';
const port = 3000;
 const server = http.createServer((req, res) => {
    res.statusCode = 200;
    res.setHeader('Content-Type', 'text/plain');
    res.end('Hello World\n');  });

 server.listen(port, hostname, () => {
    console.log(`Server running at http://${hostname}:${port}/`);
  });  

This is in contrast to today's more common concurrency model where OS threads are employed. 

  • Created by Ryan Dahl in 2009
    • Development && maintenance sponsored by Joyent
    • Licence MIT
    • Last release : 0.10.31
    • Based on Google V8 Engine
    • +99 000 packages

WHY NodeJS?


When do you use NodeJS

● Web application

● web socket app

● Ad server

● Proxy server

● Streaming server

● Fast file upload client

● Any Real-time data apps

● Anything with high I/O

● chat/messaging

● realtime applications

● high concurrency applications

● communication hubs

● recommendation systems

● MORE MORE MORE

 

 

 

 

 

 

First -- Learn basic syntax of Javascript - see CS3520

from CS3520 website (client based but, syntax the same)

 

 

 

NodeJS basics

we will learn as we go..do recommended reading

 

 

 

NodeJS modules

2 Ways to create a module

module.exports = {
    sayHelloInEnglish: function() {
      return "HELLO";    },
    sayHelloInSpanish: function() {
      return "Hola";    }
  };
// greetings.js  
 var exports = module.exports = {};
 exports.sayHelloInEnglish = function() {
    return "HELLO";  };
 exports.sayHelloInSpanish = function() {
    return "Hola";  };

 

how to import (require) a module into a NodeJS file

// main.js
                var greetings = require("./greetings.js");

// "Hello"
  greetings.sayHelloInEnglish();

// "Hola"
    greetings.sayHelloInSpanish();
The keyword require returns an object, which references the value of module.exports for a given file

 

how to install a module in your application director

$npm install <module name>

 

 

Installation, Application Setup and using Heroku

 

 

 

© Lynne Grewe