Your SlideShare is downloading. ×
0
Node.js Web App
CRASH COURSE
By Aaron Silverman, Code Slinger at Video BlocksMarch 19, 2014
Note: node.js is getting more and more popular every year!
Node.js - download and install it!
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
re...
Run simple web server
$ node server.js
Server running at http://127.0.0.1:1337/
Connect to simple web server
Why reinvent the wheel?
Packages FTW!
$ npm install -g express-generator
...
express-generator@3.0.0
/usr/local/lib/node_m...
Create skeleton web application
$ express --ejs myapp
...
create : myapp
...
install dependencies:
...
run the app:
Install dependencies
$ cd myapp
$ npm install
…
express@3.4.8 node_modules/express
...
Start web application
$ npm start
> application-name@0.0.1 start
/Users/aaron/Workspace/upenn/myapp
> node ./bin/www
Connect to web application
What’s going on?
automagically created folders and files
package.json is key
started by npm start
installed by npm install
require - import/include other files
and packages
imports debug package
imports app file
what is exported
Middleware Magic
view engine
modules
middleware
routing
Route handlers
render view
View - Embedded JavaScript
view variable
Spice up our root page - route handler
exports.index = function(req, res){
res.render('index', { title: 'Express' });
res....
Spice up our root page - view
<body>
<h1><%= title %></h1>
<p>Welcome to <%= title %></p>
<p>&ldquo;Welcome to <%= title %...
Restart and connect
$ npm start
Internet folks love cats
Cat image
image file
Cat view
<!DOCTYPE html>
<html>
<head>
<link rel='stylesheet' href='/stylesheets/style.css' />
</head>
<body>
<h1>Cat</h1>...
Cat route handler
exports.index = function(req, res) {
res.render('index', {title: 'Penn', name: 'Aaron'});
};
exports.cat...
Cat route
app.get('/', routes.index);
app.get('/users', users.list);
app.get('/cat', routes.cat);
app.js
Enough manual restarting - nodemon
$ sudo npm install -g nodemon
...
nodemon@1.0.15
…
Tell nodemon what to run
"scripts": {
"start": "node ./bin/www"
},
"main": "bin/www",
"dependencies": {
package.json
Start server using nodemon
$ nodemon
[nodemon] v1.0.15
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: *....
Navigate to cat page
Name that cat
Name that cat - route handler
exports.cat = function(req, res) {
var name = req.param('name','Mr. Cat');
res.render('cat')...
Name that cat - view
<body>
<h1>Cat</h1>
<h1>Meet <%= name %>, the Cat.</h1>
<img src="/images/cat.jpg" />
</body>
routes/...
Revisit cat page
Pass name in query parameter
Moar Cats!
Twitter Bootstrap - CSS for dummies
Bootstrap grid and components
CSS classes also makes special components
CSS class “row” starts row of a 12 column grid
CSS...
Cat view - bootstrap css/js
<head>
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.mi...
Cat view - name jumbotron
<body>
<div class="jumbotron">
<h1>Meet <%= name %>, the Cat.</h1>
<p>Best cat in the land</p>
<...
Cat view - cat grid
<div class="container cat-grid">
<div class="row">
<div class="col-md-4">
<img src="/images/cat.jpg" /...
Cat view - cat style
.cat-grid img {
height: 100px;
}
public/stylesheets/style.css
Moar, responsive, cats
Module Time
Npm install and save
$ npm install --save express-partials
npm http GET https://registry.npmjs.org/express-partials
npm ht...
Added to package.json
newly installed package
Use new middleware
var bodyParser = require('body-parser');
var partials = require('express-partials');
var routes = requi...
Create common view layout
views/layout.ejs
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="//netdna.bootstrapcd...
Clean up cat view
views/cat.ejs
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/boots...
Clean up index view
views/index.ejs
<!DOCTYPE html>
<html>
<head>
<title><%= title %></title>
<link rel='stylesheet' href=...
Revisit Pages
$ nodemon
How can Grandma see the cat?
Deploy on the interwebs!
Transfer code to your server
$ scp -i ~/.ssh/macbookair.pem -r myapp/ ubuntu@ec2-23-
20-153-76.compute-1.amazonaws.com:~/
...
Install node.js on your server
$ ssh -i .ssh/macbookair.pem ubuntu@ec2-23-20-153-76.
compute-1.amazonaws.com
$ sudo apt-ge...
Install dependencies
$ cd myapp
$ npm install
Note: If you transferred your node_modules folder (such as scp did in this p...
Allow HTTP traffic for the server
Allow port forwarding to listen port
$ sudo vim /etc/sysctl.conf
(uncomment net.ipv4.ip_forward)
$ sudo sysctl -p /etc/sys...
Start and connect to server
$ npm start
> application-name@0.0.1 start
/home/ubuntu/myapp
> node ./bin/www
Forever - run node as a daemon
$ sudo npm install -g forever
…
forever@0.10.11 /usr/lib/node_modules/forever
…
$ forever s...
Website now live forever!
Node.js & Twitter Bootstrap Crash Course
Node.js & Twitter Bootstrap Crash Course
Node.js & Twitter Bootstrap Crash Course
Node.js & Twitter Bootstrap Crash Course
Node.js & Twitter Bootstrap Crash Course
Node.js & Twitter Bootstrap Crash Course
Node.js & Twitter Bootstrap Crash Course
Node.js & Twitter Bootstrap Crash Course
Upcoming SlideShare
Loading in...5
×

Node.js & Twitter Bootstrap Crash Course

18,639

Published on

Slides from Node.js and Twitter Bootstrap crash course given to Penn Graduate Computing Club. Covers creating basic node app, using the bootstrap grid, and deploying to an EC2 machine.

Published in: Technology
2 Comments
25 Likes
Statistics
Notes
  • Very useful. One of the more practical tutorials out there for people just starting out with Node.js and Express
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Nice! It will definitely help me in my next update here: http://ieatcss.com/twitter-bootstrap-tutorial.html

    Regards,
    Hussain.
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Views
Total Views
18,639
On Slideshare
0
From Embeds
0
Number of Embeds
6
Actions
Shares
0
Downloads
200
Comments
2
Likes
25
Embeds 0
No embeds

No notes for slide

Transcript of "Node.js & Twitter Bootstrap Crash Course"

  1. 1. Node.js Web App CRASH COURSE By Aaron Silverman, Code Slinger at Video BlocksMarch 19, 2014
  2. 2. Note: node.js is getting more and more popular every year! Node.js - download and install it!
  3. 3. var http = require('http'); http.createServer(function (req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello Worldn'); }).listen(1337, '127.0.0.1'); console.log('Server running at http://127.0.0.1:1337/'); Create simple server web server server.js
  4. 4. Run simple web server $ node server.js Server running at http://127.0.0.1:1337/
  5. 5. Connect to simple web server
  6. 6. Why reinvent the wheel? Packages FTW! $ npm install -g express-generator ... express-generator@3.0.0 /usr/local/lib/node_modules/express-generator ├── mkdirp@0.3.5 └── commander@1.3.2 (keypress@0.1.0)
  7. 7. Create skeleton web application $ express --ejs myapp ... create : myapp ... install dependencies: ... run the app:
  8. 8. Install dependencies $ cd myapp $ npm install … express@3.4.8 node_modules/express ...
  9. 9. Start web application $ npm start > application-name@0.0.1 start /Users/aaron/Workspace/upenn/myapp > node ./bin/www
  10. 10. Connect to web application
  11. 11. What’s going on? automagically created folders and files
  12. 12. package.json is key started by npm start installed by npm install
  13. 13. require - import/include other files and packages imports debug package imports app file what is exported
  14. 14. Middleware Magic view engine modules middleware routing
  15. 15. Route handlers render view
  16. 16. View - Embedded JavaScript view variable
  17. 17. Spice up our root page - route handler exports.index = function(req, res){ res.render('index', { title: 'Express' }); res.render('index', {title: 'Express', name: 'Aaron'}); }; routes/index.js
  18. 18. Spice up our root page - view <body> <h1><%= title %></h1> <p>Welcome to <%= title %></p> <p>&ldquo;Welcome to <%= title %>&rdquo;</p> <p>&mdash; <%= name %><p> </body> views/index.ejs
  19. 19. Restart and connect $ npm start
  20. 20. Internet folks love cats
  21. 21. Cat image image file
  22. 22. Cat view <!DOCTYPE html> <html> <head> <link rel='stylesheet' href='/stylesheets/style.css' /> </head> <body> <h1>Cat</h1> <img src="/images/cat.jpg" /> </body> </html> views/cat.ejs
  23. 23. Cat route handler exports.index = function(req, res) { res.render('index', {title: 'Penn', name: 'Aaron'}); }; exports.cat = function(req, res) { res.render('cat'); } routes/index.js
  24. 24. Cat route app.get('/', routes.index); app.get('/users', users.list); app.get('/cat', routes.cat); app.js
  25. 25. Enough manual restarting - nodemon $ sudo npm install -g nodemon ... nodemon@1.0.15 …
  26. 26. Tell nodemon what to run "scripts": { "start": "node ./bin/www" }, "main": "bin/www", "dependencies": { package.json
  27. 27. Start server using nodemon $ nodemon [nodemon] v1.0.15 [nodemon] to restart at any time, enter `rs` [nodemon] watching: *.* [nodemon] starting `node bin/www`
  28. 28. Navigate to cat page
  29. 29. Name that cat
  30. 30. Name that cat - route handler exports.cat = function(req, res) { var name = req.param('name','Mr. Cat'); res.render('cat'); res.render('cat', {name: name}); } routes/index.js
  31. 31. Name that cat - view <body> <h1>Cat</h1> <h1>Meet <%= name %>, the Cat.</h1> <img src="/images/cat.jpg" /> </body> routes/index.js
  32. 32. Revisit cat page
  33. 33. Pass name in query parameter
  34. 34. Moar Cats!
  35. 35. Twitter Bootstrap - CSS for dummies
  36. 36. Bootstrap grid and components CSS classes also makes special components CSS class “row” starts row of a 12 column grid CSS class “col-md-N” starts a responsive N-column wide grid element Note: rows can be nested inside columns for a nested grid
  37. 37. Cat view - bootstrap css/js <head> <link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" /> <link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap-theme.min.css" /> <link rel='stylesheet' href='/stylesheets/style.css' /> </head> … <script src="//netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script> </body> </html> views/cat.ejs Note: instead of using external assets we could also have downloaded and extracted these files into the public/stylesheets and public/javscripts directories.
  38. 38. Cat view - name jumbotron <body> <div class="jumbotron"> <h1>Meet <%= name %>, the Cat.</h1> <p>Best cat in the land</p> </div> views/cat.ejs
  39. 39. Cat view - cat grid <div class="container cat-grid"> <div class="row"> <div class="col-md-4"> <img src="/images/cat.jpg" /> </div> <div class="col-md-4"> ... </div> <div class="col-md-4"> <img src="/images/cat.jpg" /> </div> </div> </div> views/cat.ejs <div class="row"> <div class="col-md-6"> <img src="/images/cat.jpg" /> </div> <div class="col-md-6"> <img src="/images/cat.jpg" /> </div> </div> <div class="row"> <div class="col-md-6"> <img src="/images/cat.jpg" /> </div> <div class="col-md-6"> <img src="/images/cat.jpg" /> </div> </div>
  40. 40. Cat view - cat style .cat-grid img { height: 100px; } public/stylesheets/style.css
  41. 41. Moar, responsive, cats
  42. 42. Module Time
  43. 43. Npm install and save $ npm install --save express-partials npm http GET https://registry.npmjs.org/express-partials npm http 304 https://registry.npmjs.org/express-partials express-partials@0.2.0 node_modules/express-partials
  44. 44. Added to package.json newly installed package
  45. 45. Use new middleware var bodyParser = require('body-parser'); var partials = require('express-partials'); var routes = require('./routes'); var users = require('./routes/user'); var app = express(); // view engine setup app.set('views', path.join(__dirname, 'views')); app.set('view engine', 'ejs'); app.use(partials()); app.use(favicon()); app.js
  46. 46. Create common view layout views/layout.ejs <!DOCTYPE html> <html> <head> <link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" /> <link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap-theme.min.css" /> <link rel='stylesheet' href='/stylesheets/style.css' /> </head> <body> <%- body %> <script src="//netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script> </body> </html>
  47. 47. Clean up cat view views/cat.ejs <!DOCTYPE html> <html> <head> <link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" /> <link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap-theme.min.css" /> <link rel='stylesheet' href='/stylesheets/style.css' /> </head> <body> … <script src="//netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script> </body> </html>
  48. 48. Clean up index view views/index.ejs <!DOCTYPE html> <html> <head> <title><%= title %></title> <link rel='stylesheet' href='/stylesheets/style.css' /> </head> <body> … </body> </html>
  49. 49. Revisit Pages $ nodemon
  50. 50. How can Grandma see the cat?
  51. 51. Deploy on the interwebs!
  52. 52. Transfer code to your server $ scp -i ~/.ssh/macbookair.pem -r myapp/ ubuntu@ec2-23- 20-153-76.compute-1.amazonaws.com:~/ Note: Ideally your code is checked into a source control repository such as git, and you would add your server’s public key to be allowed to access to the repository. Then you would do something like: $ ssh -i .ssh/macbookair.pem ubuntu@ec2-23-20-153-76.compute-1.amazonaws.com $ git clone git@github.com:Username/myapp.git
  53. 53. Install node.js on your server $ ssh -i .ssh/macbookair.pem ubuntu@ec2-23-20-153-76. compute-1.amazonaws.com $ sudo apt-get install python-software-properties $ sudo apt-add-repository ppa:chris-lea/node.js $ sudo apt-get update $ sudo apt-get install nodejs $ node --version v0.10.26
  54. 54. Install dependencies $ cd myapp $ npm install Note: If you transferred your node_modules folder (such as scp did in this presentation) this won’t be necessary. However typically that folder should be ignored by version control (e.g. in .gitignore) and you will have to do this step.
  55. 55. Allow HTTP traffic for the server
  56. 56. Allow port forwarding to listen port $ sudo vim /etc/sysctl.conf (uncomment net.ipv4.ip_forward) $ sudo sysctl -p /etc/sysctl.conf net.ipv4.ip_forward = 1 $ sudo iptables -A PREROUTING -t nat -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 3000
  57. 57. Start and connect to server $ npm start > application-name@0.0.1 start /home/ubuntu/myapp > node ./bin/www
  58. 58. Forever - run node as a daemon $ sudo npm install -g forever … forever@0.10.11 /usr/lib/node_modules/forever … $ forever start bin/www warn: --minUptime not set. Defaulting to: 1000ms warn: --spinSleepTime not set. Your script will exit if it does not stay up for at least 1000ms info: Forever processing file: bin/www
  59. 59. Website now live forever!
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×