Lighthouse Labs logo
Javascript Essentials

Introductions

Lighthouse Labs logo

Who are we?

How do we do that?

Full-time Immersive Bootcamps

Web or iOS Development Bootcamp

Part-time Courses

Front End Fundamentals with Javascript, Intro to Web or Intro to iOS Development

Workshops and Events

The Goal

Finished App

Setup

  1. Modern Web Browser (Chrome preferred)
  2. A Code Editor (Sublime or VS Code)
  3. Node.js Installed
Note: If you cannot install Node, you can use an online environment such as repl.it

Checking Node Version

Input

                            
                                node --version
                            
                        

Output

                            
                                v10.15.1
                            
                        
👋 If you don't see a version number or are getting an error, please raise your hand.

HTML

The most fundamental language used on the Web

Getting Started Pt1

  1. Make a directory to store your project.

                                            
                                                mkdir chat-app
                                            
                                        
  2. Navigate into that folder and create index.html.

                                            
                                                cd chat-app
                                                touch index.html
                                            
                                        

Getting Started Pt2

Finished App

Getting Started Pt3

  1. Add the following HTML code to index.html.

                                            
                                                <!DOCTYPE html>
                                                <html lang="en">
                                                <head>
                                                    <title>Chat App</title>
                                                </head>
                                                <body>
                                                    <main>
                                                        <ol id="history">
                                                            <li>KV says: hey there, DV</li>
                                                            <li>DV says: Hiii!!!</li>
                                                        </ol>
                                                        <form>
                                                            <input id="initials">
                                                            <input id="message">
                                                            <button>Send</button>
                                                        </form>
                                                    </main>
                                                </body>
                                                </html>
                                            
                                        

Getting Started Pt4

  1. Open index.html in a browser.

no styles

CSS

Cascading Style Sheets

Styling Pt1

  1. Create a file called style.css.

                                            
                                                touch style.css
                                            
                                        
  2. Add a link to style.css in the head of index.html.

                                            
                                                <!DOCTYPE html>
                                                <html lang="en">
                                                <head>
                                                    <title>Chat App</title>
                                                    <link rel="stylesheet" href="style.css">
                                                </head>
                                            
                                        

Styling Pt2

  1. Add the following CSS code to style.css.

                                            
                                                body {
                                                    background-color: white;
                                                    color: #303030;
                                                    font-family: 'Helvetica Neue', Arial, sans-serif;
                                                    font-size: 15px;
                                                    margin: 20px;
                                                }
                                                
                                                main {
                                                    background-color: #f0f0f0;
                                                    border-radius: 6px;
                                                    box-shadow: 3px 3px 12px #c8c8c8;
                                                    margin: 20px auto;
                                                    max-width: 470px;
                                                    padding: 20px;
                                                }
                                            
                                        

Styling Pt3

  1. This is what we are trying to achieve:

Finished App

JavaScript

The World's Most Popular Programming Language

Client Side JS Pt1

  1. Create a file called app.js.

                                            
                                                touch app.js
                                            
                                        
  2. Add these tags to the bottom of index.html.

                                            
                                                    </main>
                                                    <script src="https://code.jquery.com/jquery-3.1.1.js"></script>
                                                    <script src="app.js"></script>
                                                </body>
                                                </html>
                                            
                                        

Client Side JS Pt2

  1. Add this code to app.js.

                                            
                                                alert('hello from the JS file');
                                            
                                        
  2. Refresh your index.html.

    alert popup

Client Side JS Pt3

Functions

                            
                                alert('hello from the JS file');
                            
                        
  • Call or invoke a function by putting parenteses () after it

  • Similar to math functions

  • Take in some values, do some crunching, and give you back a computed value or some behaviour

Client Side JS Pt4

  1. Add this code to app.js.

                                            
                                                $('form').on('submit',function () {
                                                    var text = $('#message').val();
                                                    alert(text);
                                                    return false;
                                                });
                                            
                                        
  2. Refresh index.html.

    alert popup

Client Side JS Pt5

Code Explanation

                            
                                $('form').on('submit',function () {
                                    var text = $('#message').val();
                                    alert(text);
                                    return false;
                                });
                            
                        
  • Access jQuery with the dollar sign $

  • Target the form element with $('form')

  • Listen for an event with .on('name of event')

  • .on is a function that accepts another function as a parameter

Client Side JS Pt6

Code Explanation

                            
                                $('form').on('submit',function () {
                                    var text = $('#message').val();
                                    alert(text);
                                    return false;
                                });
                            
                        
  • The rest of the code is indented so we know it's inside a function

  • Capture the text value of an HTML element with the id of "message"

  • Then pass that text to the alert function

Client Side JS Pt7

Code Explanation

                            
                                $('form').on('submit',function () {
                                    var text = $('#message').val();
                                    alert(text);
                                    return false;
                                });
                            
                        
  • Return false to tell the browser to cancel the form submission

Node.js

JavaScript on the Server Side

Getting Started with Node Pt1

  1. Create a new folder called client.

                                            
                                                mkdir client
                                            
                                        
  2. Move all the "client" side files into the new folder.

    client folder

Getting Started with Node Pt2

  1. Create a new file called server.js.

                                            
                                                touch server.js
                                            
                                        
  2. Client and server.

    client and server

Getting Started with Node Pt3

  1. Add this code to server.js.

                                            
                                                console.log('hello from our node script!');
                                            
                                        
  2. Run the file.

                                            
                                                node server.js
                                            
                                        

Getting Started with Node Pt4

  1. Output

                                            
                                                hello from our node script!
                                            
                                        

Express

Our Web Server

Web Server Pt1

  1. Initialize the folder.

                                            
                                                npm init -y
                                            
                                        
  2. Install Express.

                                            
                                                npm install express
                                            
                                        

Web Server Pt2

  1. Add this code to server.js.

                                            
                                                var express = require('express');
                                                var app = express();
                                                
                                                var http = require('http');
                                                var server = http.Server(app);
                                                
                                                app.use(express.static('client'));
                                                
                                                server.listen(8080, function() {
                                                    console.log('Chat server running');
                                                });
                                            
                                        

Web Server Pt3

Code Explanation

                            
                                var express = require('express');
                                var app = express();
                                
                                var http = require('http');
                                var server = http.Server(app);
                                
                                app.use(express.static('client'));
                                
                                server.listen(8080, function() {
                                    console.log('Chat server running');
                                });
                            
                        
  • Load external modules with require('name-of-module').

Web Server Pt4

Code Explanation

                            
                                var express = require('express');
                                var app = express();
                                
                                var http = require('http');
                                var server = http.Server(app);
                                
                                app.use(express.static('client'));
                                
                                server.listen(8080, function() {
                                    console.log('Chat server running');
                                });
                            
                        
  • Serve static content with express.static('path-to-folder')

Web Server Pt5

Code Explanation

                            
                                var express = require('express');
                                var app = express();
                                
                                var http = require('http');
                                var server = http.Server(app);
                                
                                app.use(express.static('client'));
                                
                                server.listen(8080, function() {
                                    console.log('Chat server running');
                                });
                            
                        
  • Tell the server to listen on a port with server.listen(portNumber)

Web Server Pt6

  1. Start the server.

                                            
                                                node server.js
                                            
                                        
  2. Visit http://localhost:8080 in your browser.

Web Server Pt7

chat app

Socket.io

For Real-Time Messaging

Adding Sockets Pt1

  1. Install Socket.IO.

                                            
                                                npm install socket.io
                                            
                                        
  2. Add this code to server.js.

                                            
                                                app.use(express.static('client'));
                                                
                                                var io = require('socket.io')(server);
                                                
                                                io.on('connection', function (socket) {
                                                    socket.on('message', function (msg) {
                                                        io.emit('message', msg);
                                                    });
                                                });
                                                
                                                server.listen(8080, function () {
                                                    console.log('Chat server running');
                                                });
                                            
                                        

Adding Socket.io Pt2

  1. Reference Socket.IO.

                                            
                                                <script src="https://code.jquery.com/jquery-3.1.1.js"></script>
                                                <script src="/socket.io/socket.io.js"></script>
                                                <script src="app.js"></script>
                                            
                                        
  2. Add this code to the top of app.js.

                                            
                                                var socket = io();
                                                
                                                $('form').on('submit', function () {
                                            
                                        

Adding Socket.io Pt3

  1. Update app.js with this code.

                                            
                                                var text = $('#message').val();
                                                socket.emit('message', text);
                                                $('#message').val('');
                                                return false;
                                            
                                        
  2. Add this code to the bottom of app.js.

                                            
                                                socket.on('message', function (msg) {
                                                    $('<li>').text(msg).appendTo('#history');
                                                });
                                            
                                        

Adding Socket.io Pt4

Final code for app.js

                            
                                var socket = io();
                                
                                $('form').on('submit', function () {
                                    var text = $('#message').val();
                                    socket.emit('message', text);
                                    $('#message').val('');
                                    return false;
                                });
                                
                                socket.on('message', function (msg) {
                                    $('<li>').text(msg).appendTo('#history');
                                });
                            
                        

Adding Socket.io Pt5

Final code for server.js

                            
                                var express = require('express');
                                var app = express();
                                
                                var http = require('http');
                                var server = http.Server(app);
                                
                                app.use(express.static('client'));
                                
                                var io = require('socket.io')(server);
                                
                                io.on('connection', function (socket) {
                                    socket.on('message', function (msg) {
                                        io.emit('message', msg);
                                    });
                                });
                                
                                server.listen(8080, function() {
                                    console.log('Chat server running');
                                });
                            
                        

Adding Socket.io Pt6

  1. Start the server.

                                            
                                                node server.js
                                            
                                        

The Complete App

finished app

Enhancements As Challenges

  1. Modify app.js so that it emits the initials field as well.

  2. Make it so only a maximum of two characters can be entered into initials field.

  3. Add a list of all previous messages.

Review

  • HTML & CSS

  • jQuery

  • Node

  • Express

  • Socket.IO

Continuing Your Learning