Wifi: Lighthouse Labs
Password: 401WGeorgia
Who are we?
How do we do that?
Full-time Immersive Bootcamps
Web or iOS Development BootcampPart-time Courses
Front End Fundamentals with Javascript, Intro to Web or Intro to iOS DevelopmentWorkshops and Events
Input
node --version
Output
v10.15.1
The most fundamental language used on the Web
Make a directory to store your project.
mkdir chat-app
Navigate into that folder and create index.html.
cd chat-app
touch index.html
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>
Open index.html in a browser.
Cascading Style Sheets
Create a file called style.css.
touch style.css
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>
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;
}
This is what we are trying to achieve:
The World's Most Popular Programming Language
Create a file called app.js.
touch app.js
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>
Add this code to app.js.
alert('hello from the JS file');
Refresh your index.html.
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
Add this code to app.js.
$('form').on('submit',function () {
var text = $('#message').val();
alert(text);
return false;
});
Refresh index.html.
$('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
$('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
$('form').on('submit',function () {
var text = $('#message').val();
alert(text);
return false;
});
Return false to tell the browser to cancel the form submission
JavaScript on the Server Side
Create a new folder called client.
mkdir client
Move all the "client" side files into the new folder.
Create a new file called server.js.
touch server.js
Client and server.
Add this code to server.js.
console.log('hello from our node script!');
Run the file.
node server.js
Output
hello from our node script!
Our Web Server
Initialize the folder.
npm init -y
Install Express.
npm install express
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');
});
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').
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')
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)
Start the server.
node server.js
Visit http://localhost:8080 in your browser.
For Real-Time Messaging
Install Socket.IO.
npm install socket.io
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');
});
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>
Add this code to the top of app.js.
var socket = io();
$('form').on('submit', function () {
Update app.js with this code.
var text = $('#message').val();
socket.emit('message', text);
$('#message').val('');
return false;
Add this code to the bottom of app.js.
socket.on('message', function (msg) {
$('<li>').text(msg).appendTo('#history');
});
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');
});
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');
});
Start the server.
node server.js
Modify app.js so that it emits the initials field as well.
Make it so only a maximum of two characters can be entered into initials field.
Add a list of all previous messages.
HTML & CSS
jQuery
Node
Express
Socket.IO
Lighthouse Labs Prep Course