WebFund 2015W Lecture 5: Difference between revisions
Created page with "==Topics & Readings== ==Audio & Video== The video and audio are available below (separately, unfortunately): * Video in [http://homeostasis.scs.carleton.ca/~soma/webfund-20..." |
No edit summary |
||
Line 1: | Line 1: | ||
==Topics & Readings== | ==Topics & Readings== | ||
"JavaScript: The Good Parts", Ch. 3-4 objects and functions | |||
==Audio & Video== | ==Audio & Video== | ||
Line 9: | Line 11: | ||
==Notes== | ==Notes== | ||
<h3>Running the form-demo application and using the debug module</h3> | |||
In the terminal, navigate to the folder containing the demo and type: | |||
<table> | |||
<tr> | |||
<td style="padding-right: 50px;"><code>> DEBUG= '*' bin/www</code></td> | |||
<td>To see all debugging messages</td> | |||
</tr> | |||
<tr> | |||
<td style="padding-right: 50px;"><code>> DEBUG= 'form*' bin/www</code></td> | |||
<td>To see all debugging messages that start with “form”</td> | |||
</tr> | |||
<tr> | |||
<td style="padding-right: 50px;"> | |||
<p><code>> cd bin</code></p> | |||
<p><code>> emacs www &</code></p> | |||
</td> | |||
<td rowspan="2">To view the application code in a text editor.</td> | |||
</tr> | |||
</table> | |||
With the application running, in the address bar of a web browser, type: | |||
<table> | |||
<tr> | |||
<td style="padding-right: 50px;">http://localhost:3000 </td> | |||
<td>To go to the main page of the form-demo</td> | |||
</tr> | |||
<tr> | |||
<td style="padding-right: 50px;">http://localhost:3000/list</td> | |||
<td>To go to a page which shows the form info that has been added to the list</td> | |||
</tr> | |||
</table> | |||
<h3>Within the bin/www file</h3> | |||
<ul> | |||
<li>The line <code>var debug = require('debug')('form-demo:server');</code> imports the debug module needed to print debug messages.</li> | |||
<li>The <code>debug()</code> function can be used to print debug messages as seen on the line <code>debug(“Listening on” + bind);</code></li> | |||
<li>The scope of <code>var app</code>, <code>var debug</code> and <code>var http</code> is the file.</li> | |||
</ul> | |||
<h3>Within the app.js file</h3> | |||
<ul> | |||
<li>In the line <code>module.exports = app;</code>, “module” is nowhere else in the file, so it is built in.</li> | |||
<li><code>require('…');</code> loads a file, evaluates it, and expects the file to do something with <code>module.exports</code>.</li> | |||
<li>A file may only export one object, but that object can hold as much information(attributes) as you want. In this file, app is being exported.</li> | |||
<li>In a file, everything is private except for the one object you export.</li> | |||
</ul> | |||
<h3>Using Node's built-in debugger</h3> | |||
<ul> | |||
<li>Node's debugger can be used to debug code by stopping it in the middle of its execution so that you may examine what is happening at a specific point.</li> | |||
<li>You first place <code>debugger</code> at the points in the code you wish to analyze further.</li> | |||
<li>Next, run the application in a terminal with the debugger. For an application called app, this would look like <code>node debug app</code>.</li> | |||
<li>The debugger will stop at the first line of code and enter a REPL (read, evaluate, print loop) in which you can enter any valid JavaScript code, usually to to examine or modify variables.</li> | |||
<li>Type <code>ctrl+c</code> to break from the loop.</li> | |||
<li>The program's execution will pause at every point in the code you placed a <code>debugger</code> statement.</li> | |||
<li>When not in REPL, commands such as s(step in), o(step out) and c(continue) can be used to move through the execution of the program.</li> | |||
<li>If you choose the continue command, execution will run until a <code>debugger</code> break point is encountered.</li> | |||
</ul> | |||
<h4>Example (within routes/index.js file)</h4> | |||
Code snippet: | |||
<code><pre> | |||
router.post('/add', function(req, res){ | |||
… | |||
state.push(obj); | |||
debugger; //*** Put this at the place in the code that you want to be debugged. | |||
res.render... | |||
}); | |||
</pre></code> | |||
<ul> | |||
<li><code>debugger</code> tells Node's debugger to stop running code when it reaches that point, so that you can see exactly what is happening at that place.</li> | |||
<li>To start the debugger, type <code>node debug ./bin/www</code> in the terminal.</li> | |||
<li>Type <code>ctrl+c</code> to exit the REPL and then type <code>c</code> to continue the execution of the code up to the <code>debugger</code> point.</li> | |||
<li>In the terminal, the node debugger can be used to change any values in the application.</li> | |||
<li>When the deugger has hit the break point, type <code>state[0].country = “Canada”;</code></li> | |||
<li>In this case you are changing information that has been entered by the user in the form from within the Node debugger.</li> | |||
</ul> | |||
<h3>Jade template language</h3> | |||
<ul> | |||
<li>The Jade template engine is used in Node.js to render Jade files which are web page templates written in the Jade language.</li> | |||
<li>The rendering process produces HTML pages from the Jade templates.</li> | |||
<li>You don’t need to close tags. Eg. <code>label Name</code>.</li> | |||
<li>But spacing and tabs matter! Ensure that all indentation is correct and never mix spaces and tabs for indentation.</li> | |||
</ul> | |||
<h3>Within the index.js file</h3> | |||
<ul> | |||
<li><code>var express = require('express');</code> is declared at the top of the file. This loads the express module which is then used to set up a router object.</li> | |||
<li>The different files of an application are linked together through the use of the <code>require</code> call to load the files as modules.</li> | |||
<li><code>var router = express.Router();</code> sets up a router to do routing on incoming requests.</li> | |||
<li><code>router.get</code> is a router method used to deal with GET requests. | |||
<ul> | |||
<li>Eg: <code>router.get('/', function(req, res, next)</code></li> | |||
<li>This handles GET requests to the specified path (/) with the specified callback function. The callback function is executed every time a request of this type comes in.</li> | |||
<li>The req parameter of the callback function is the request object, res is the response object and next is used to pass the request on to another function.</li> | |||
</ul> | |||
</li> | |||
<li><code>router.post</code> is a router method used to deal with POST requests. | |||
<ul> | |||
<li>Eg: <code>router.post('/add', function(req, res)</code></li> | |||
<li>This handles POST requests to the specified path (/add) with the specified callback function.</li> | |||
</ul> | |||
</li> | |||
<li><code>router.post</code> means "response render" | |||
<ul> | |||
<li>Eg: <code>res.render('add', {title: 'Person just added', item: obj });</code></li> | |||
<li>This renders the Jade template specified in the first parameter (add).</li> | |||
<li>The second parameter is an object passed to the Jade template. This is used to provide dynamic data to the page upon rendering.</li> | |||
<li>The Jade templates must be located in the location specified to the application (typically the views directory). This is set in the app.js file along with the line which specifies that Jade is to be used as the rendering engine.</li> | |||
</ul> | |||
</li> | |||
</ul> | |||
<h3>Within views/index.jade file</h3> | |||
<ul> | |||
<li>index.jade is a Jade template. </li> | |||
<li><code>extends layout</code> says that this is a template that extends another template called "layout".</li> | |||
<li>layout.jade can be extended at the locations in which it has the <code>block</code> keyword followed by a name.</li> | |||
<li>If a block is undefined in the extending template, that is ok. It will leave that block empty.</li> | |||
<li>Everything indented under <code>block content</code> will go in the location of the block named content in the extended template.</li> | |||
<li>Why can we have a <code>block header</code> and a <code>block content</code>? | |||
<ul> | |||
<li>Sometimes you want to add things in different areas of a page. For example in the head of the page, you may want to add things like JavaScript or stylesheets. Then in the body of the page you may want to add other content.</li> | |||
<li>In the class example a reference to a style sheet was added in the head.</li> | |||
</ul> | |||
</li> | |||
</ul> | |||
<h3>IMPORTANT NOTES</h3> | |||
<ul> | |||
<li>Go through this basic form application... for the midterm you should know everything in these files. </li> | |||
<li>To learn and figure out what things do, start going through it, and try to break it. Eg. Comment out <code>app.use (‘/’, routes);</code> then the 404 error message appears when you try to reload the webpage.</li> | |||
</ul> |
Revision as of 04:07, 28 January 2015
Topics & Readings
"JavaScript: The Good Parts", Ch. 3-4 objects and functions
Audio & Video
The video and audio are available below (separately, unfortunately):
Notes
Running the form-demo application and using the debug module
In the terminal, navigate to the folder containing the demo and type:
> DEBUG= '*' bin/www |
To see all debugging messages |
> DEBUG= 'form*' bin/www |
To see all debugging messages that start with “form” |
|
To view the application code in a text editor. |
With the application running, in the address bar of a web browser, type:
http://localhost:3000 | To go to the main page of the form-demo |
http://localhost:3000/list | To go to a page which shows the form info that has been added to the list |
Within the bin/www file
- The line
var debug = require('debug')('form-demo:server');
imports the debug module needed to print debug messages. - The
debug()
function can be used to print debug messages as seen on the linedebug(“Listening on” + bind);
- The scope of
var app
,var debug
andvar http
is the file.
Within the app.js file
- In the line
module.exports = app;
, “module” is nowhere else in the file, so it is built in. require('…');
loads a file, evaluates it, and expects the file to do something withmodule.exports
.- A file may only export one object, but that object can hold as much information(attributes) as you want. In this file, app is being exported.
- In a file, everything is private except for the one object you export.
Using Node's built-in debugger
- Node's debugger can be used to debug code by stopping it in the middle of its execution so that you may examine what is happening at a specific point.
- You first place
debugger
at the points in the code you wish to analyze further. - Next, run the application in a terminal with the debugger. For an application called app, this would look like
node debug app
. - The debugger will stop at the first line of code and enter a REPL (read, evaluate, print loop) in which you can enter any valid JavaScript code, usually to to examine or modify variables.
- Type
ctrl+c
to break from the loop. - The program's execution will pause at every point in the code you placed a
debugger
statement. - When not in REPL, commands such as s(step in), o(step out) and c(continue) can be used to move through the execution of the program.
- If you choose the continue command, execution will run until a
debugger
break point is encountered.
Example (within routes/index.js file)
Code snippet:
router.post('/add', function(req, res){
…
state.push(obj);
debugger; //*** Put this at the place in the code that you want to be debugged.
res.render...
});
debugger
tells Node's debugger to stop running code when it reaches that point, so that you can see exactly what is happening at that place.- To start the debugger, type
node debug ./bin/www
in the terminal. - Type
ctrl+c
to exit the REPL and then typec
to continue the execution of the code up to thedebugger
point. - In the terminal, the node debugger can be used to change any values in the application.
- When the deugger has hit the break point, type
state[0].country = “Canada”;
- In this case you are changing information that has been entered by the user in the form from within the Node debugger.
Jade template language
- The Jade template engine is used in Node.js to render Jade files which are web page templates written in the Jade language.
- The rendering process produces HTML pages from the Jade templates.
- You don’t need to close tags. Eg.
label Name
. - But spacing and tabs matter! Ensure that all indentation is correct and never mix spaces and tabs for indentation.
Within the index.js file
var express = require('express');
is declared at the top of the file. This loads the express module which is then used to set up a router object.- The different files of an application are linked together through the use of the
require
call to load the files as modules. var router = express.Router();
sets up a router to do routing on incoming requests.router.get
is a router method used to deal with GET requests.- Eg:
router.get('/', function(req, res, next)
- This handles GET requests to the specified path (/) with the specified callback function. The callback function is executed every time a request of this type comes in.
- The req parameter of the callback function is the request object, res is the response object and next is used to pass the request on to another function.
- Eg:
router.post
is a router method used to deal with POST requests.- Eg:
router.post('/add', function(req, res)
- This handles POST requests to the specified path (/add) with the specified callback function.
- Eg:
router.post
means "response render"- Eg:
res.render('add', {title: 'Person just added', item: obj });
- This renders the Jade template specified in the first parameter (add).
- The second parameter is an object passed to the Jade template. This is used to provide dynamic data to the page upon rendering.
- The Jade templates must be located in the location specified to the application (typically the views directory). This is set in the app.js file along with the line which specifies that Jade is to be used as the rendering engine.
- Eg:
Within views/index.jade file
- index.jade is a Jade template.
extends layout
says that this is a template that extends another template called "layout".- layout.jade can be extended at the locations in which it has the
block
keyword followed by a name. - If a block is undefined in the extending template, that is ok. It will leave that block empty.
- Everything indented under
block content
will go in the location of the block named content in the extended template. - Why can we have a
block header
and ablock content
?- Sometimes you want to add things in different areas of a page. For example in the head of the page, you may want to add things like JavaScript or stylesheets. Then in the body of the page you may want to add other content.
- In the class example a reference to a style sheet was added in the head.
IMPORTANT NOTES
- Go through this basic form application... for the midterm you should know everything in these files.
- To learn and figure out what things do, start going through it, and try to break it. Eg. Comment out
app.use (‘/’, routes);
then the 404 error message appears when you try to reload the webpage.