├── .gitignore ├── README.md ├── diagnostic ├── .gitignore ├── dbcon.js.template ├── diagnostic.js ├── package.json └── views │ ├── 404.handlebars │ ├── 500.handlebars │ ├── home.handlebars │ └── layouts │ └── main.handlebars ├── express-forms ├── forms-demo.js ├── package.json └── views │ ├── 404.handlebars │ ├── 500.handlebars │ ├── get-loopback-improved.handlebars │ ├── get-loopback.handlebars │ ├── home.handlebars │ ├── layouts │ └── main.handlebars │ ├── post-loopback.handlebars │ └── show-data.handlebars ├── express-http ├── .gitignore ├── credentials.js.template ├── helloHttp.js ├── helloOrganization.js ├── package.json ├── public │ └── css │ │ └── style.css └── views │ ├── 404.handlebars │ ├── 500.handlebars │ ├── home.handlebars │ └── layouts │ └── main.handlebars ├── express-mysql ├── .gitignore ├── dbcon.js ├── dbcon.js.template ├── helloMysql.js ├── package.json └── views │ ├── 404.handlebars │ ├── 500.handlebars │ ├── home.handlebars │ └── layouts │ └── main.handlebars ├── express-sessions ├── helloSessions.js ├── intSessions.js ├── package.json └── views │ ├── 404.handlebars │ ├── 500.handlebars │ ├── counter.handlebars │ ├── layouts │ └── main.handlebars │ ├── newSession.handlebars │ └── toDo.handlebars ├── hello-express ├── helloHandlebars.js ├── helloWorld.js ├── package.json ├── tree.txt └── views │ ├── 404.handlebars │ ├── 500.handlebars │ ├── home.handlebars │ ├── layouts │ └── main.handlebars │ ├── other-page.handlebars │ └── time.handlebars └── hello-world ├── helloWorld.js └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | **/node_modules 2 | **/credentials.js -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # CS290-Server-Side-Examples 2 | -------------------------------------------------------------------------------- /diagnostic/.gitignore: -------------------------------------------------------------------------------- 1 | dbcon.js 2 | -------------------------------------------------------------------------------- /diagnostic/dbcon.js.template: -------------------------------------------------------------------------------- 1 | var mysql = require('mysql'); 2 | var pool = mysql.createPool({ 3 | connectionLimit : 10, 4 | host : 'mysql.eecs.oregonstate.edu', 5 | user : 'cs290_yourengrusername', 6 | password : 'last-4-digits-of-your-osu-id', 7 | database : 'cs290_yourengrusername' 8 | }); 9 | 10 | module.exports.pool = pool; 11 | -------------------------------------------------------------------------------- /diagnostic/diagnostic.js: -------------------------------------------------------------------------------- 1 | var express = require('express'); 2 | var mysql = require('./dbcon.js'); 3 | 4 | var app = express(); 5 | var handlebars = require('express-handlebars').create({defaultLayout:'main'}); 6 | 7 | app.engine('handlebars', handlebars.engine); 8 | app.set('view engine', 'handlebars'); 9 | app.set('port', process.argv[2]); 10 | 11 | app.get('/',function(req,res,next){ 12 | var context = {}; 13 | var createString = "CREATE TABLE diagnostic(" + 14 | "id INT PRIMARY KEY AUTO_INCREMENT," + 15 | "text VARCHAR(255) NOT NULL)"; 16 | mysql.pool.query('DROP TABLE IF EXISTS diagnostic', function(err){ 17 | if(err){ 18 | next(err); 19 | return; 20 | } 21 | mysql.pool.query(createString, function(err){ 22 | if(err){ 23 | next(err); 24 | return; 25 | } 26 | mysql.pool.query('INSERT INTO diagnostic (`text`) VALUES ("MySQL is Working!")',function(err){ 27 | mysql.pool.query('SELECT * FROM diagnostic', function(err, rows, fields){ 28 | context.results = JSON.stringify(rows); 29 | res.render('home',context); 30 | }); 31 | }); 32 | }); 33 | }); 34 | }); 35 | 36 | app.use(function(req,res){ 37 | res.status(404); 38 | res.render('404'); 39 | }); 40 | 41 | app.use(function(err, req, res, next){ 42 | console.error(err.stack); 43 | res.status(500); 44 | res.render('500'); 45 | }); 46 | 47 | app.listen(app.get('port'), function(){ 48 | console.log('Express started on http://localhost:' + app.get('port') + '; press Ctrl-C to terminate.'); 49 | }); 50 | -------------------------------------------------------------------------------- /diagnostic/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "hello-world", 3 | "version": "1.0.0", 4 | "description": "Hello world with express and node.js", 5 | "main": "app.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "author": "OSU", 10 | "license": "ISC", 11 | "dependencies": { 12 | "body-parser": "^1.13.3", 13 | "express": "^4.13.2", 14 | "express-handlebars": "^2.0.1", 15 | "express-session": "^1.11.3", 16 | "mysql": "^2.8.0" 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /diagnostic/views/404.handlebars: -------------------------------------------------------------------------------- 1 |

Error 404 - Page Is Nowhere to be Found

-------------------------------------------------------------------------------- /diagnostic/views/500.handlebars: -------------------------------------------------------------------------------- 1 |

Error 500 - Something Has Gone Terribly Wrong

-------------------------------------------------------------------------------- /diagnostic/views/home.handlebars: -------------------------------------------------------------------------------- 1 |

MySQL Results:

2 |

{{results}}

-------------------------------------------------------------------------------- /diagnostic/views/layouts/main.handlebars: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Demo Page 5 | 6 | 7 | {{{body}}} 8 | 9 | -------------------------------------------------------------------------------- /express-forms/forms-demo.js: -------------------------------------------------------------------------------- 1 | var express = require('express'); 2 | 3 | var app = express(); 4 | var handlebars = require('express-handlebars').create({defaultLayout:'main'}); 5 | var bodyParser = require('body-parser'); 6 | 7 | app.use(bodyParser.urlencoded({ extended: false })); 8 | app.use(bodyParser.json()); 9 | 10 | app.engine('handlebars', handlebars.engine); 11 | app.set('view engine', 'handlebars'); 12 | app.set('port', 3000); 13 | 14 | app.get('/',function(req,res){ 15 | res.render('home'); 16 | }); 17 | 18 | app.get('/show-data',function(req,res){ 19 | var context = {}; 20 | context.sentData = req.query.myData; 21 | res.render('show-data', context); 22 | }); 23 | 24 | app.get('/get-loopback',function(req,res){ 25 | var qParams = ""; 26 | for (var p in req.query){ 27 | qParams += "The name " + p + " contains the value " + req.query[p] + ", "; 28 | } 29 | qParams = qParams.substring(0,qParams.lastIndexOf(',')); 30 | qParams += '.'; 31 | var context = {}; 32 | context.dataList = qParams; 33 | res.render('get-loopback', context); 34 | }); 35 | 36 | app.get('/get-loopback-improved',function(req,res){ 37 | var qParams = []; 38 | for (var p in req.query){ 39 | qParams.push({'name':p,'value':req.query[p]}) 40 | } 41 | var context = {}; 42 | context.dataList = qParams; 43 | res.render('get-loopback-improved', context); 44 | }); 45 | 46 | app.post('/post-loopback', function(req,res){ 47 | var qParams = []; 48 | for (var p in req.body){ 49 | qParams.push({'name':p,'value':req.body[p]}) 50 | } 51 | console.log(qParams); 52 | console.log(req.body); 53 | var context = {}; 54 | context.dataList = qParams; 55 | res.render('post-loopback', context); 56 | }); 57 | 58 | app.use(function(req,res){ 59 | res.status(404); 60 | res.render('404'); 61 | }); 62 | 63 | app.use(function(err, req, res, next){ 64 | console.error(err.stack); 65 | res.type('plain/text'); 66 | res.status(500); 67 | res.render('500'); 68 | }); 69 | 70 | app.listen(app.get('port'), function(){ 71 | console.log('Express started on http://localhost:' + app.get('port') + '; press Ctrl-C to terminate.'); 72 | }); 73 | -------------------------------------------------------------------------------- /express-forms/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "hello-world", 3 | "version": "1.0.0", 4 | "description": "Hello world with express and node.js", 5 | "main": "app.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "author": "OSU", 10 | "license": "ISC", 11 | "dependencies": { 12 | "body-parser": "^1.13.3", 13 | "express": "^4.13.2", 14 | "express-handlebars": "^2.0.1", 15 | "express-session": "^1.11.3", 16 | "mysql": "^2.8.0" 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /express-forms/views/404.handlebars: -------------------------------------------------------------------------------- 1 |

Error 404 - Page Is Nowhere to be Found

-------------------------------------------------------------------------------- /express-forms/views/500.handlebars: -------------------------------------------------------------------------------- 1 |

Error 500 - Something Has Gone Terribly Wrong

-------------------------------------------------------------------------------- /express-forms/views/get-loopback-improved.handlebars: -------------------------------------------------------------------------------- 1 |

You sent the following data in a GET request:

2 | -------------------------------------------------------------------------------- /express-forms/views/get-loopback.handlebars: -------------------------------------------------------------------------------- 1 |

You sent the following data: {{dataList}}

-------------------------------------------------------------------------------- /express-forms/views/home.handlebars: -------------------------------------------------------------------------------- 1 |

The Home Page

-------------------------------------------------------------------------------- /express-forms/views/layouts/main.handlebars: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Demo Page 5 | 6 | 7 | {{{body}}} 8 | 9 | -------------------------------------------------------------------------------- /express-forms/views/post-loopback.handlebars: -------------------------------------------------------------------------------- 1 |

You sent the following data in a POST request:

2 | -------------------------------------------------------------------------------- /express-forms/views/show-data.handlebars: -------------------------------------------------------------------------------- 1 |

The data you sent in the variable myData is: {{sentData}}

-------------------------------------------------------------------------------- /express-http/.gitignore: -------------------------------------------------------------------------------- 1 | credentials.js -------------------------------------------------------------------------------- /express-http/credentials.js.template: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | owmKey: 'YourKeyHere' 3 | } -------------------------------------------------------------------------------- /express-http/helloHttp.js: -------------------------------------------------------------------------------- 1 | var express = require('express'); 2 | 3 | var app = express(); 4 | var handlebars = require('express-handlebars').create({defaultLayout:'main'}); 5 | var credentials = require('./credentials.js'); 6 | var request = require('request'); 7 | 8 | app.engine('handlebars', handlebars.engine); 9 | app.set('view engine', 'handlebars'); 10 | app.set('port', 3000); 11 | app.use(express.static('public')); 12 | 13 | app.get('/',function(req,res,next){ 14 | var context = {}; 15 | request('http://api.openweathermap.org/data/2.5/weather?q=corvallis&APPID=' + credentials.owmKey, function(err, response, body){ 16 | if(!err && response.statusCode < 400){ 17 | context.owm = body; 18 | request({ 19 | "url":"http://httpbin.org/post", 20 | "method":"POST", 21 | "headers":{ 22 | "Content-Type":"application/json" 23 | }, 24 | "body":'{"foo":"bar","number":1}' 25 | }, function(err, response, body){ 26 | if(!err && response.statusCode < 400){ 27 | context.httpbin = body; 28 | res.render('home',context); 29 | }else{ 30 | console.log(err); 31 | if(response){ 32 | console.log(response.statusCode); 33 | } 34 | next(err); 35 | } 36 | }); 37 | } else { 38 | console.log(err); 39 | if(response){ 40 | console.log(response.statusCode); 41 | } 42 | next(err); 43 | } 44 | }); 45 | }); 46 | 47 | app.get('/get-ex',function(req,res,next){ 48 | var context = {}; 49 | request('http://api.openweathermap.org/data/2.5/weather?q=corvallis&APPID=' + credentials.owmKey, function(err, response, body){ 50 | if(!err && response.statusCode < 400){ 51 | context.owm = body; 52 | res.render('home',context); 53 | } else { 54 | if(response){ 55 | console.log(response.statusCode); 56 | } 57 | next(err); 58 | } 59 | }); 60 | }); 61 | 62 | app.use(function(req,res){ 63 | res.status(404); 64 | res.render('404'); 65 | }); 66 | 67 | app.use(function(err, req, res, next){ 68 | console.error(err.stack); 69 | res.status(500); 70 | res.render('500'); 71 | }); 72 | 73 | app.listen(app.get('port'), function(){ 74 | console.log('Express started on http://localhost:' + app.get('port') + '; press Ctrl-C to terminate.'); 75 | }); 76 | -------------------------------------------------------------------------------- /express-http/helloOrganization.js: -------------------------------------------------------------------------------- 1 | var express = require('express'); 2 | 3 | var app = express(); 4 | var handlebars = require('express-handlebars').create({defaultLayout:'main'}); 5 | var credentials = require('./credentials.js'); 6 | var request = require('request'); 7 | 8 | app.engine('handlebars', handlebars.engine); 9 | app.set('view engine', 'handlebars'); 10 | app.set('port', 3000); 11 | app.use(express.static('public')); 12 | 13 | app.get('/',function(req,res){ 14 | var context = {}; 15 | request('http://api.openweathermap.org/data/2.5/weather?q=corvallis&APPID=' + credentials.owmKey, handleGet); 16 | 17 | function handleGet(err, response, body){ 18 | if(!err && response.statusCode < 400){ 19 | context.owm = body; 20 | request({ 21 | "url":"http://httpbin.org/post", 22 | "method":"POST", 23 | "headers":{ 24 | "Content-Type":"application/json" 25 | }, 26 | "body":'{"foo":"bar","number":1}' 27 | }, handlePost) 28 | } else { 29 | console.log(err); 30 | console.log(response.statusCode); 31 | } 32 | } 33 | 34 | function handlePost(err, response, body){ 35 | if(!err && response.statusCode < 400){ 36 | context.httpbin = body; 37 | res.render('home',context); 38 | }else{ 39 | console.log(err); 40 | console.log(response.statusCode); 41 | } 42 | } 43 | }); 44 | 45 | app.use(function(req,res){ 46 | res.status(404); 47 | res.render('404'); 48 | }); 49 | 50 | app.use(function(err, req, res, next){ 51 | console.error(err.stack); 52 | res.type('plain/text'); 53 | res.status(500); 54 | res.render('500'); 55 | }); 56 | 57 | app.listen(app.get('port'), function(){ 58 | console.log('Express started on http://localhost:' + app.get('port') + '; press Ctrl-C to terminate.'); 59 | }); 60 | -------------------------------------------------------------------------------- /express-http/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "hello-world", 3 | "version": "1.0.0", 4 | "description": "Hello world with express and node.js", 5 | "main": "app.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "author": "OSU", 10 | "license": "ISC", 11 | "dependencies": { 12 | "body-parser": "^1.13.3", 13 | "express": "^4.13.2", 14 | "express-handlebars": "^2.0.1", 15 | "express-session": "^1.11.3", 16 | "mysql": "^2.8.0", 17 | "request": "^2.65.0" 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /express-http/public/css/style.css: -------------------------------------------------------------------------------- 1 | pre{ 2 | background-color: #d9d9d9; 3 | border-radius: 10px; 4 | padding: 10px; 5 | overflow-x: scroll; 6 | } -------------------------------------------------------------------------------- /express-http/views/404.handlebars: -------------------------------------------------------------------------------- 1 |

Error 404 - Page Is Nowhere to be Found

-------------------------------------------------------------------------------- /express-http/views/500.handlebars: -------------------------------------------------------------------------------- 1 |

Error 500 - Something Has Gone Terribly Wrong

-------------------------------------------------------------------------------- /express-http/views/home.handlebars: -------------------------------------------------------------------------------- 1 |

The response from Open Weather Map is:

2 |
{{owm}}
3 |

The response from httpbin is:

4 |
{{httpbin}}
-------------------------------------------------------------------------------- /express-http/views/layouts/main.handlebars: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Demo Page 5 | 6 | 7 | 8 | {{{body}}} 9 | 10 | -------------------------------------------------------------------------------- /express-mysql/.gitignore: -------------------------------------------------------------------------------- 1 | dbcon.js 2 | -------------------------------------------------------------------------------- /express-mysql/dbcon.js: -------------------------------------------------------------------------------- 1 | var mysql = require('mysql'); 2 | var pool = mysql.createPool({ 3 | connectionLimit : 10, 4 | host : '192.168.1.28', 5 | user : 'hellouser', 6 | password : 'default', 7 | database : 'hellodb' 8 | }); 9 | 10 | module.exports.pool = pool; -------------------------------------------------------------------------------- /express-mysql/dbcon.js.template: -------------------------------------------------------------------------------- 1 | var mysql = require('mysql'); 2 | var pool = mysql.createPool({ 3 | connectionLimit : 10, 4 | host : 'mysql.eecs.oregonstate.edu', 5 | user : 'cs290_yourengrusername', 6 | password : 'last-4-digits-of-your-osu-id', 7 | database : 'cs290_yourengrusername' 8 | }); 9 | 10 | module.exports.pool = pool; 11 | -------------------------------------------------------------------------------- /express-mysql/helloMysql.js: -------------------------------------------------------------------------------- 1 | var express = require('express'); 2 | var mysql = require('./dbcon.js'); 3 | 4 | var app = express(); 5 | var handlebars = require('express-handlebars').create({defaultLayout:'main'}); 6 | 7 | app.engine('handlebars', handlebars.engine); 8 | app.set('view engine', 'handlebars'); 9 | app.set('port', 3000); 10 | 11 | app.get('/',function(req,res,next){ 12 | var context = {}; 13 | mysql.pool.query('SELECT * FROM todo', function(err, rows, fields){ 14 | if(err){ 15 | next(err); 16 | return; 17 | } 18 | context.results = JSON.stringify(rows); 19 | res.render('home', context); 20 | }); 21 | }); 22 | 23 | app.get('/insert',function(req,res,next){ 24 | var context = {}; 25 | mysql.pool.query("INSERT INTO todo (`name`) VALUES (?)", [req.query.c], function(err, result){ 26 | if(err){ 27 | next(err); 28 | return; 29 | } 30 | context.results = "Inserted id " + result.insertId; 31 | res.render('home',context); 32 | }); 33 | }); 34 | 35 | app.get('/delete',function(req,res,next){ 36 | var context = {}; 37 | mysql.pool.query("DELETE FROM todo WHERE id=?", [req.query.id], function(err, result){ 38 | if(err){ 39 | next(err); 40 | return; 41 | } 42 | context.results = "Deleted " + result.changedRows + " rows."; 43 | res.render('home',context); 44 | }); 45 | }); 46 | 47 | 48 | ///simple-update?id=2&name=The+Task&done=false&due=2015-12-5 49 | app.get('/simple-update',function(req,res,next){ 50 | var context = {}; 51 | mysql.pool.query("UPDATE todo SET name=?, done=?, due=? WHERE id=? ", 52 | [req.query.name, req.query.done, req.query.due, req.query.id], 53 | function(err, result){ 54 | if(err){ 55 | next(err); 56 | return; 57 | } 58 | context.results = "Updated " + result.changedRows + " rows."; 59 | res.render('home',context); 60 | }); 61 | }); 62 | 63 | ///safe-update?id=1&name=The+Task&done=false 64 | app.get('/safe-update',function(req,res,next){ 65 | var context = {}; 66 | mysql.pool.query("SELECT * FROM todo WHERE id=?", [req.query.id], function(err, result){ 67 | if(err){ 68 | next(err); 69 | return; 70 | } 71 | if(result.length == 1){ 72 | var curVals = result[0]; 73 | mysql.pool.query("UPDATE todo SET name=?, done=?, due=? WHERE id=? ", 74 | [req.query.name || curVals.name, req.query.done || curVals.done, req.query.due || curVals.due, req.query.id], 75 | function(err, result){ 76 | if(err){ 77 | next(err); 78 | return; 79 | } 80 | context.results = "Updated " + result.changedRows + " rows."; 81 | res.render('home',context); 82 | }); 83 | } 84 | }); 85 | }); 86 | 87 | app.get('/reset-table',function(req,res,next){ 88 | var context = {}; 89 | mysql.pool.query("DROP TABLE IF EXISTS todo", function(err){ 90 | var createString = "CREATE TABLE todo(" + 91 | "id INT PRIMARY KEY AUTO_INCREMENT," + 92 | "name VARCHAR(255) NOT NULL," + 93 | "done BOOLEAN," + 94 | "due DATE)"; 95 | mysql.pool.query(createString, function(err){ 96 | context.results = "Table reset"; 97 | res.render('home',context); 98 | }) 99 | }); 100 | }); 101 | 102 | app.use(function(req,res){ 103 | res.status(404); 104 | res.render('404'); 105 | }); 106 | 107 | app.use(function(err, req, res, next){ 108 | console.error(err.stack); 109 | res.status(500); 110 | res.render('500'); 111 | }); 112 | 113 | app.listen(app.get('port'), function(){ 114 | console.log('Express started on http://localhost:' + app.get('port') + '; press Ctrl-C to terminate.'); 115 | }); 116 | -------------------------------------------------------------------------------- /express-mysql/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "hello-world", 3 | "version": "1.0.0", 4 | "description": "Hello world with express and node.js", 5 | "main": "app.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "author": "OSU", 10 | "license": "ISC", 11 | "dependencies": { 12 | "body-parser": "^1.13.3", 13 | "express": "^4.13.2", 14 | "express-handlebars": "^2.0.1", 15 | "express-session": "^1.11.3", 16 | "mysql": "^2.8.0" 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /express-mysql/views/404.handlebars: -------------------------------------------------------------------------------- 1 |

Error 404 - Page Is Nowhere to be Found

-------------------------------------------------------------------------------- /express-mysql/views/500.handlebars: -------------------------------------------------------------------------------- 1 |

Error 500 - Something Has Gone Terribly Wrong

-------------------------------------------------------------------------------- /express-mysql/views/home.handlebars: -------------------------------------------------------------------------------- 1 |

MySQL Results:

2 |

{{results}}

-------------------------------------------------------------------------------- /express-mysql/views/layouts/main.handlebars: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Demo Page 5 | 6 | 7 | {{{body}}} 8 | 9 | -------------------------------------------------------------------------------- /express-sessions/helloSessions.js: -------------------------------------------------------------------------------- 1 | var express = require('express'); 2 | 3 | var app = express(); 4 | var handlebars = require('express-handlebars').create({defaultLayout:'main'}); 5 | var session = require('express-session'); 6 | var bodyParser = require('body-parser'); 7 | 8 | app.use(bodyParser.urlencoded({ extended: false })); 9 | app.use(session({secret:'SuperSecretPassword'})); 10 | 11 | app.engine('handlebars', handlebars.engine); 12 | app.set('view engine', 'handlebars'); 13 | app.set('port', 3000); 14 | 15 | app.get('/count',function(req,res){ 16 | var context = {}; 17 | context.count = req.session.count || 0; 18 | req.session.count = context.count + 1; 19 | res.render('counter', context); 20 | }); 21 | 22 | app.post('/count',function(req,res){ 23 | var context = {}; 24 | if(req.body.command === "resetCount"){ 25 | //req.session.count = 0; 26 | req.session.destroy(); 27 | } else { 28 | context.err = true; 29 | } 30 | if(req.session){ 31 | context.count = req.session.count; 32 | } else { 33 | context.count = 0; 34 | } 35 | req.session.count = context.count + 1; 36 | res.render('counter', context); 37 | }); 38 | 39 | app.use(function(req,res){ 40 | res.status(404); 41 | res.render('404'); 42 | }); 43 | 44 | app.use(function(err, req, res, next){ 45 | console.error(err.stack); 46 | res.type('plain/text'); 47 | res.status(500); 48 | res.render('500'); 49 | }); 50 | 51 | app.listen(app.get('port'), function(){ 52 | console.log('Express started on http://localhost:' + app.get('port') + '; press Ctrl-C to terminate.'); 53 | }); 54 | -------------------------------------------------------------------------------- /express-sessions/intSessions.js: -------------------------------------------------------------------------------- 1 | var express = require('express'); 2 | 3 | var app = express(); 4 | var handlebars = require('express-handlebars').create({defaultLayout:'main'}); 5 | var session = require('express-session'); 6 | var bodyParser = require('body-parser'); 7 | 8 | app.use(bodyParser.urlencoded({ extended: false })); 9 | app.use(session({secret:'SuperSecretPassword'})); 10 | 11 | app.engine('handlebars', handlebars.engine); 12 | app.set('view engine', 'handlebars'); 13 | app.set('port', 3000); 14 | 15 | app.get('/',function(req,res,next){ 16 | var context = {}; 17 | //If there is no session, go to the main page. 18 | if(!req.session.name){ 19 | res.render('newSession', context); 20 | return; 21 | } 22 | context.name = req.session.name; 23 | context.toDoCount = req.session.toDo.length || 0; 24 | context.toDo = req.session.toDo || []; 25 | console.log(context.toDo); 26 | res.render('toDo',context); 27 | }); 28 | 29 | app.post('/',function(req,res){ 30 | var context = {}; 31 | 32 | if(req.body['New List']){ 33 | req.session.name = req.body.name; 34 | req.session.toDo = []; 35 | req.session.curId = 0; 36 | } 37 | 38 | //If there is no session, go to the main page. 39 | if(!req.session.name){ 40 | res.render('newSession', context); 41 | return; 42 | } 43 | 44 | if(req.body['Add Item']){ 45 | req.session.toDo.push({"name":req.body.name, "id":req.session.curId}); 46 | req.session.curId++; 47 | } 48 | 49 | if(req.body['Done']){ 50 | req.session.toDo = req.session.toDo.filter(function(e){ 51 | return e.id != req.body.id; 52 | }) 53 | } 54 | 55 | context.name = req.session.name; 56 | context.toDoCount = req.session.toDo.length; 57 | context.toDo = req.session.toDo; 58 | console.log(context.toDo); 59 | res.render('toDo',context); 60 | }); 61 | 62 | app.use(function(req,res){ 63 | res.status(404); 64 | res.render('404'); 65 | }); 66 | 67 | app.use(function(err, req, res, next){ 68 | console.error(err.stack); 69 | res.type('plain/text'); 70 | res.status(500); 71 | res.render('500'); 72 | }); 73 | 74 | app.listen(app.get('port'), function(){ 75 | console.log('Express started on http://localhost:' + app.get('port') + '; press Ctrl-C to terminate.'); 76 | }); 77 | -------------------------------------------------------------------------------- /express-sessions/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "hello-world", 3 | "version": "1.0.0", 4 | "description": "Hello world with express and node.js", 5 | "main": "app.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "author": "OSU", 10 | "license": "ISC", 11 | "dependencies": { 12 | "body-parser": "^1.13.3", 13 | "express": "^4.13.2", 14 | "express-handlebars": "^2.0.1", 15 | "express-session": "^1.11.3", 16 | "mysql": "^2.8.0" 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /express-sessions/views/404.handlebars: -------------------------------------------------------------------------------- 1 |

Error 404 - Page Is Nowhere to be Found

-------------------------------------------------------------------------------- /express-sessions/views/500.handlebars: -------------------------------------------------------------------------------- 1 |

Error 500 - Something Has Gone Terribly Wrong

-------------------------------------------------------------------------------- /express-sessions/views/counter.handlebars: -------------------------------------------------------------------------------- 1 | {{#unless err}} 2 |

Hello! You have visited this page {{count}} times before.

3 |
4 | 5 |
6 | {{else}} 7 |

Invalid submission!

8 | {{/unless}} 9 | -------------------------------------------------------------------------------- /express-sessions/views/layouts/main.handlebars: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Demo Page 5 | 6 | 7 | {{{body}}} 8 | 9 | -------------------------------------------------------------------------------- /express-sessions/views/newSession.handlebars: -------------------------------------------------------------------------------- 1 |
2 | 3 | 4 | 5 |
-------------------------------------------------------------------------------- /express-sessions/views/toDo.handlebars: -------------------------------------------------------------------------------- 1 |

Hello {{name}} Here is your To Do List!

2 |

You have {{toDoCount}} items on your to do list.

3 |
4 | 5 | 6 |
7 | {{#if toDo}} 8 | 19 | {{/if}} -------------------------------------------------------------------------------- /hello-express/helloHandlebars.js: -------------------------------------------------------------------------------- 1 | var express = require('express'); 2 | 3 | var app = express(); 4 | var handlebars = require('express-handlebars').create({defaultLayout:'main'}); 5 | 6 | app.engine('handlebars', handlebars.engine); 7 | app.set('view engine', 'handlebars'); 8 | app.set('port', 3000); 9 | 10 | app.get('/',function(req,res){ 11 | res.render('home'); 12 | }); 13 | 14 | app.get('/other-page',function(req,res){ 15 | res.render('other-page'); 16 | }); 17 | 18 | 19 | function genContext(){ 20 | var stuffToDisplay = {}; 21 | stuffToDisplay.time = (new Date(Date.now())).toLocaleTimeString('en-US'); 22 | return stuffToDisplay; 23 | } 24 | 25 | app.get('/time',function(req,res){ 26 | res.render('time', genContext()); 27 | }); 28 | 29 | app.use(function(req,res){ 30 | res.status(404); 31 | res.render('404'); 32 | }); 33 | 34 | app.use(function(err, req, res, next){ 35 | console.error(err.stack); 36 | res.type('plain/text'); 37 | res.status(500); 38 | res.render('500'); 39 | }); 40 | 41 | app.listen(app.get('port'), function(){ 42 | console.log('Express started on http://localhost:' + app.get('port') + '; press Ctrl-C to terminate.'); 43 | }); 44 | -------------------------------------------------------------------------------- /hello-express/helloWorld.js: -------------------------------------------------------------------------------- 1 | var express = require('express'); 2 | 3 | var app = express(); 4 | 5 | app.set('port', 3000); 6 | 7 | app.get('/',function(req,res){ 8 | res.type('text/plain'); 9 | res.send('Welcome to the main page!'); 10 | }); 11 | 12 | app.get('/other-page',function(req,res){ 13 | res.type('text/plain'); 14 | res.send('Welcome to the other page!'); 15 | }); 16 | 17 | app.use(function(req,res){ 18 | res.type('text/plain'); 19 | res.status(404); 20 | res.send('404 - Not Found'); 21 | }); 22 | 23 | app.use(function(err, req, res, next){ 24 | console.error(err.stack); 25 | res.type('plain/text'); 26 | res.status(500); 27 | res.send('500 - Server Error'); 28 | }); 29 | 30 | app.listen(app.get('port'), function(){ 31 | console.log('Express started on http://localhost:' + app.get('port') + '; press Ctrl-C to terminate.'); 32 | }); 33 | -------------------------------------------------------------------------------- /hello-express/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "hello-world", 3 | "version": "1.0.0", 4 | "description": "Hello world with express and node.js", 5 | "main": "app.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "author": "OSU", 10 | "license": "ISC", 11 | "dependencies": { 12 | "body-parser": "^1.13.3", 13 | "express": "^4.13.2", 14 | "express-handlebars": "^2.0.1", 15 | "express-session": "^1.11.3", 16 | "mysql": "^2.8.0" 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /hello-express/tree.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wolfordj/CS290-Server-Side-Examples/f66c2ef45ad7fb9e759b5bb1a9f0d9c5fc987ff4/hello-express/tree.txt -------------------------------------------------------------------------------- /hello-express/views/404.handlebars: -------------------------------------------------------------------------------- 1 |

Error 404 - Page Is Nowhere to be Found

-------------------------------------------------------------------------------- /hello-express/views/500.handlebars: -------------------------------------------------------------------------------- 1 |

Error 500 - Something Has Gone Terribly Wrong

-------------------------------------------------------------------------------- /hello-express/views/home.handlebars: -------------------------------------------------------------------------------- 1 |

The Home Page

-------------------------------------------------------------------------------- /hello-express/views/layouts/main.handlebars: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Demo Page 5 | 6 | 7 | {{{body}}} 8 | 9 | -------------------------------------------------------------------------------- /hello-express/views/other-page.handlebars: -------------------------------------------------------------------------------- 1 |

This is not the home page

-------------------------------------------------------------------------------- /hello-express/views/time.handlebars: -------------------------------------------------------------------------------- 1 |

The current time is {{time}}

-------------------------------------------------------------------------------- /hello-world/helloWorld.js: -------------------------------------------------------------------------------- 1 | var http = require('http'); 2 | 3 | http.createServer(function(req,res){ 4 | res.writeHead(200, { 'Content-Type': 'text/plain' }); 5 | res.end('Hello world!'); 6 | }).listen(3000); 7 | 8 | console.log('Server started on localhost:3000; press Ctrl-C to terminate....'); 9 | -------------------------------------------------------------------------------- /hello-world/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "hello-world", 3 | "version": "1.0.0", 4 | "description": "Hello world with express and node.js", 5 | "main": "app.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "author": "OSU", 10 | "license": "ISC", 11 | "dependencies": { 12 | "body-parser": "^1.13.3", 13 | "express": "^4.13.2", 14 | "express-handlebars": "^2.0.1", 15 | "express-session": "^1.11.3", 16 | "mysql": "^2.8.0" 17 | } 18 | } 19 | --------------------------------------------------------------------------------