├── .gitignore
├── 01-first-app
└── server.js
├── 02-nodejs-javascript
├── app.js
├── module1.js
└── module2.js
├── 03-node-only-render-html
├── index.html
└── server.js
├── 04-node-only-routing
├── app.js
├── index.html
├── login.html
└── server.js
├── 05-express-first-app
├── app.js
├── bin
│ └── www
├── npm-debug.log
├── package.json
├── public
│ └── stylesheets
│ │ └── style.css
├── routes
│ └── index.js
└── views
│ ├── error.jade
│ ├── index.jade
│ └── layout.jade
├── 06-handlebars
├── app.js
├── bin
│ └── www
├── package.json
├── public
│ └── stylesheets
│ │ └── style.css
├── routes
│ └── index.js
└── views
│ ├── error.hbs
│ ├── index.hbs
│ └── layouts
│ └── layout.hbs
├── 07-get-post
├── app.js
├── bin
│ └── www
├── package.json
├── public
│ └── stylesheets
│ │ └── style.css
├── routes
│ └── index.js
└── views
│ ├── error.hbs
│ ├── index.hbs
│ ├── layouts
│ └── layout.hbs
│ └── test.hbs
├── 08-validation-sessions
├── .gitignore
├── app.js
├── bin
│ └── www
├── package.json
├── public
│ └── stylesheets
│ │ └── style.css
├── routes
│ └── index.js
└── views
│ ├── error.hbs
│ ├── index.hbs
│ ├── layouts
│ └── layout.hbs
│ └── test.hbs
├── 09-mongodb
├── .gitignore
├── app.js
├── bin
│ └── www
├── package.json
├── public
│ └── stylesheets
│ │ └── style.css
├── routes
│ └── index.js
└── views
│ ├── error.hbs
│ ├── index.hbs
│ └── layouts
│ └── layout.hbs
├── 10-monk
├── .gitignore
├── app.js
├── bin
│ └── www
├── package.json
├── public
│ └── stylesheets
│ │ └── style.css
├── routes
│ └── index.js
└── views
│ ├── error.hbs
│ ├── index.hbs
│ └── layouts
│ └── layout.hbs
├── 11-mongoose
├── .gitignore
├── app.js
├── bin
│ └── www
├── package.json
├── public
│ └── stylesheets
│ │ └── style.css
├── routes
│ └── index.js
└── views
│ ├── error.hbs
│ ├── index.hbs
│ └── layouts
│ └── layout.hbs
├── LICENSE.MD
└── README.MD
/.gitignore:
--------------------------------------------------------------------------------
1 | /.idea
2 | /node_modules
--------------------------------------------------------------------------------
/01-first-app/server.js:
--------------------------------------------------------------------------------
1 | var http = require('http');
2 |
3 | function onRequest(request, response) {
4 | response.writeHead(200, {'Content-Type': 'text/plain'});
5 | response.write('Hello World');
6 | response.end();
7 | }
8 |
9 | http.createServer(onRequest).listen(8000);
--------------------------------------------------------------------------------
/02-nodejs-javascript/app.js:
--------------------------------------------------------------------------------
1 | var http = require('http');
2 | var module1 = require('./module1');
3 | var module2 = require('./module2');
4 |
5 | function onRequest(request, response) {
6 | response.writeHead(200, {'Content-Type': 'text/plain'});
7 | response.write(module2.myVariable);
8 | module2.myFunction();
9 | response.end();
10 | }
11 |
12 | http.createServer(onRequest).listen(8000);
--------------------------------------------------------------------------------
/02-nodejs-javascript/module1.js:
--------------------------------------------------------------------------------
1 | function myFunction() {
2 | console.log('Function was called');
3 | }
4 |
5 | var myString = 'String!';
6 |
7 | module.exports.myFunction = myFunction;
8 | module.exports.myString = myString;
--------------------------------------------------------------------------------
/02-nodejs-javascript/module2.js:
--------------------------------------------------------------------------------
1 | module.exports = {
2 | myFunction: function () {
3 | console.log('Exported!');
4 | },
5 | myVariable: 'Exported Variable'
6 | };
--------------------------------------------------------------------------------
/03-node-only-render-html/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | My File
6 |
7 |
8 | Now using HTML
9 |
10 |
--------------------------------------------------------------------------------
/03-node-only-render-html/server.js:
--------------------------------------------------------------------------------
1 | var http = require('http');
2 | var fs = require('fs');
3 |
4 | function onRequest(request, response) {
5 | response.writeHead(200, {'Content-Type': 'text/html'});
6 | fs.readFile('./index.html', null, function(error, data) {
7 | if (error) {
8 | response.writeHead(404);
9 | response.write('File not found!');
10 | } else {
11 | response.write(data);
12 | }
13 | response.end();
14 | });
15 | }
16 |
17 | http.createServer(onRequest).listen(8000);
--------------------------------------------------------------------------------
/04-node-only-routing/app.js:
--------------------------------------------------------------------------------
1 | var url = require('url');
2 | var fs = require('fs');
3 |
4 | function renderHTML(path, response) {
5 | fs.readFile(path, null, function(error, data) {
6 | if (error) {
7 | response.writeHead(404);
8 | response.write('File not found!');
9 | } else {
10 | response.write(data);
11 | }
12 | response.end();
13 | });
14 | }
15 |
16 | module.exports = {
17 | handleRequest: function(request, response) {
18 | response.writeHead(200, {'Content-Type': 'text/html'});
19 |
20 | var path = url.parse(request.url).pathname;
21 | switch (path) {
22 | case '/':
23 | renderHTML('./index.html', response);
24 | break;
25 | case '/login':
26 | renderHTML('./login.html', response);
27 | break;
28 | default:
29 | response.writeHead(404);
30 | response.write('Route not defined');
31 | response.end();
32 | }
33 |
34 | }
35 | };
--------------------------------------------------------------------------------
/04-node-only-routing/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Test Doc
6 |
7 |
8 | It works!
9 |
10 |
--------------------------------------------------------------------------------
/04-node-only-routing/login.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Login
6 |
7 |
8 | The Login Page
9 |
10 |
--------------------------------------------------------------------------------
/04-node-only-routing/server.js:
--------------------------------------------------------------------------------
1 | var http = require('http');
2 | var app = require('./app');
3 |
4 | http.createServer(app.handleRequest).listen(8000);
5 |
--------------------------------------------------------------------------------
/05-express-first-app/app.js:
--------------------------------------------------------------------------------
1 | var express = require('express');
2 | var path = require('path');
3 | var favicon = require('serve-favicon');
4 | var logger = require('morgan');
5 | var cookieParser = require('cookie-parser');
6 | var bodyParser = require('body-parser');
7 |
8 | var routes = require('./routes/index');
9 |
10 | var app = express();
11 |
12 | // view engine setup
13 | app.set('views', path.join(__dirname, 'views'));
14 | app.set('view engine', 'jade');
15 |
16 | // uncomment after placing your favicon in /public
17 | //app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
18 | app.use(logger('dev'));
19 | app.use(bodyParser.json());
20 | app.use(bodyParser.urlencoded({ extended: false }));
21 | app.use(cookieParser());
22 | app.use(express.static(path.join(__dirname, 'public')));
23 |
24 | app.use('/', routes);
25 |
26 | // catch 404 and forward to error handler
27 | app.use(function(req, res, next) {
28 | var err = new Error('Not Found');
29 | err.status = 404;
30 | next(err);
31 | });
32 |
33 | // error handlers
34 |
35 | // development error handler
36 | // will print stacktrace
37 | if (app.get('env') === 'development') {
38 | app.use(function(err, req, res, next) {
39 | res.status(err.status || 500);
40 | res.render('error', {
41 | message: err.message,
42 | error: err
43 | });
44 | });
45 | }
46 |
47 | // production error handler
48 | // no stacktraces leaked to user
49 | app.use(function(err, req, res, next) {
50 | res.status(err.status || 500);
51 | res.render('error', {
52 | message: err.message,
53 | error: {}
54 | });
55 | });
56 |
57 |
58 | module.exports = app;
59 |
--------------------------------------------------------------------------------
/05-express-first-app/bin/www:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env node
2 |
3 | /**
4 | * Module dependencies.
5 | */
6 |
7 | var app = require('../app');
8 | var debug = require('debug')('05-express-first-app:server');
9 | var http = require('http');
10 |
11 | /**
12 | * Get port from environment and store in Express.
13 | */
14 |
15 | var port = normalizePort(process.env.PORT || '8000');
16 | app.set('port', port);
17 |
18 | /**
19 | * Create HTTP server.
20 | */
21 |
22 | var server = http.createServer(app);
23 |
24 | /**
25 | * Listen on provided port, on all network interfaces.
26 | */
27 |
28 | server.listen(port);
29 | server.on('error', onError);
30 | server.on('listening', onListening);
31 |
32 | /**
33 | * Normalize a port into a number, string, or false.
34 | */
35 |
36 | function normalizePort(val) {
37 | var port = parseInt(val, 10);
38 |
39 | if (isNaN(port)) {
40 | // named pipe
41 | return val;
42 | }
43 |
44 | if (port >= 0) {
45 | // port number
46 | return port;
47 | }
48 |
49 | return false;
50 | }
51 |
52 | /**
53 | * Event listener for HTTP server "error" event.
54 | */
55 |
56 | function onError(error) {
57 | if (error.syscall !== 'listen') {
58 | throw error;
59 | }
60 |
61 | var bind = typeof port === 'string'
62 | ? 'Pipe ' + port
63 | : 'Port ' + port;
64 |
65 | // handle specific listen errors with friendly messages
66 | switch (error.code) {
67 | case 'EACCES':
68 | console.error(bind + ' requires elevated privileges');
69 | process.exit(1);
70 | break;
71 | case 'EADDRINUSE':
72 | console.error(bind + ' is already in use');
73 | process.exit(1);
74 | break;
75 | default:
76 | throw error;
77 | }
78 | }
79 |
80 | /**
81 | * Event listener for HTTP server "listening" event.
82 | */
83 |
84 | function onListening() {
85 | var addr = server.address();
86 | var bind = typeof addr === 'string'
87 | ? 'pipe ' + addr
88 | : 'port ' + addr.port;
89 | debug('Listening on ' + bind);
90 | }
91 |
--------------------------------------------------------------------------------
/05-express-first-app/npm-debug.log:
--------------------------------------------------------------------------------
1 | 0 info it worked if it ends with ok
2 | 1 verbose cli [ '/usr/local/bin/node', '/usr/local/bin/npm', 'start' ]
3 | 2 info using npm@3.7.3
4 | 3 info using node@v5.6.0
5 | 4 verbose stack Error: Failed to parse json
6 | 4 verbose stack Unexpected token 'c' at 17:2
7 | 4 verbose stack }cl
8 | 4 verbose stack ^
9 | 4 verbose stack at parseError (/usr/local/lib/node_modules/npm/node_modules/read-package-json/read-json.js:379:11)
10 | 4 verbose stack at parseJson (/usr/local/lib/node_modules/npm/node_modules/read-package-json/read-json.js:68:23)
11 | 4 verbose stack at /usr/local/lib/node_modules/npm/node_modules/read-package-json/read-json.js:48:5
12 | 4 verbose stack at /usr/local/lib/node_modules/npm/node_modules/graceful-fs/graceful-fs.js:78:16
13 | 4 verbose stack at tryToString (fs.js:414:3)
14 | 4 verbose stack at FSReqWrap.readFileAfterClose [as oncomplete] (fs.js:401:12)
15 | 5 verbose cwd /Users/maximilianschwarzmuller/Projects/Web Development/tutorials/youtube/nodejs/basics/05-express-first-app
16 | 6 error Darwin 15.3.0
17 | 7 error argv "/usr/local/bin/node" "/usr/local/bin/npm" "start"
18 | 8 error node v5.6.0
19 | 9 error npm v3.7.3
20 | 10 error file /Users/maximilianschwarzmuller/Projects/Web Development/tutorials/youtube/nodejs/basics/05-express-first-app/package.json
21 | 11 error code EJSONPARSE
22 | 12 error Failed to parse json
23 | 12 error Unexpected token 'c' at 17:2
24 | 12 error }cl
25 | 12 error ^
26 | 13 error File: /Users/maximilianschwarzmuller/Projects/Web Development/tutorials/youtube/nodejs/basics/05-express-first-app/package.json
27 | 14 error Failed to parse package.json data.
28 | 14 error package.json must be actual JSON, not just JavaScript.
29 | 14 error
30 | 14 error This is not a bug in npm.
31 | 14 error Tell the package author to fix their package.json file. JSON.parse
32 | 15 verbose exit [ 1, true ]
33 |
--------------------------------------------------------------------------------
/05-express-first-app/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "05-express-first-app",
3 | "version": "0.0.0",
4 | "private": true,
5 | "scripts": {
6 | "start": "node ./bin/www"
7 | },
8 | "dependencies": {
9 | "body-parser": "~1.13.2",
10 | "cookie-parser": "~1.3.5",
11 | "debug": "~2.2.0",
12 | "express": "~4.13.1",
13 | "jade": "~1.11.0",
14 | "morgan": "~1.6.1",
15 | "serve-favicon": "~2.3.0"
16 | }
17 | }
--------------------------------------------------------------------------------
/05-express-first-app/public/stylesheets/style.css:
--------------------------------------------------------------------------------
1 | body {
2 | padding: 50px;
3 | font: 14px "Lucida Grande", Helvetica, Arial, sans-serif;
4 | }
5 |
6 | a {
7 | color: #00B7FF;
8 | }
9 |
--------------------------------------------------------------------------------
/05-express-first-app/routes/index.js:
--------------------------------------------------------------------------------
1 | var express = require('express');
2 | var router = express.Router();
3 |
4 | /* GET home page. */
5 | router.get('/', function(req, res, next) {
6 | res.render('index', { title: 'Cool, huh!', condition: false });
7 | });
8 |
9 | /* GET users listing. */
10 | router.get('/users', function(req, res, next) {
11 | res.send('respond with a resource');
12 | });
13 |
14 | router.get('/users/detail', function(req, res, next) {
15 | res.send('detail');
16 | });
17 |
18 |
19 | module.exports = router;
20 |
--------------------------------------------------------------------------------
/05-express-first-app/views/error.jade:
--------------------------------------------------------------------------------
1 | extends layout
2 |
3 | block content
4 | h1= message
5 | h2= error.status
6 | pre #{error.stack}
7 |
--------------------------------------------------------------------------------
/05-express-first-app/views/index.jade:
--------------------------------------------------------------------------------
1 | extends layout
2 |
3 | block content
4 | h1= title
5 | p Welcome to #{title}
6 |
--------------------------------------------------------------------------------
/05-express-first-app/views/layout.jade:
--------------------------------------------------------------------------------
1 | doctype html
2 | html
3 | head
4 | title= title
5 | link(rel='stylesheet', href='/stylesheets/style.css')
6 | body
7 | block content
8 |
--------------------------------------------------------------------------------
/06-handlebars/app.js:
--------------------------------------------------------------------------------
1 | var express = require('express');
2 | var path = require('path');
3 | var favicon = require('serve-favicon');
4 | var logger = require('morgan');
5 | var cookieParser = require('cookie-parser');
6 | var bodyParser = require('body-parser');
7 | var hbs = require('express-handlebars');
8 |
9 | var routes = require('./routes/index');
10 |
11 | var app = express();
12 |
13 | // view engine setup
14 | app.engine('hbs', hbs({extname: 'hbs', defaultLayout: 'layout', layoutsDir: __dirname + '/views/layouts/'}));
15 | app.set('views', path.join(__dirname, 'views'));
16 | app.set('view engine', 'hbs');
17 |
18 | // uncomment after placing your favicon in /public
19 | //app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
20 | app.use(logger('dev'));
21 | app.use(bodyParser.json());
22 | app.use(bodyParser.urlencoded({ extended: false }));
23 | app.use(cookieParser());
24 | app.use(express.static(path.join(__dirname, 'public')));
25 |
26 | app.use('/', routes);
27 |
28 | // catch 404 and forward to error handler
29 | app.use(function(req, res, next) {
30 | var err = new Error('Not Found');
31 | err.status = 404;
32 | next(err);
33 | });
34 |
35 | // error handlers
36 |
37 | // development error handler
38 | // will print stacktrace
39 | if (app.get('env') === 'development') {
40 | app.use(function(err, req, res, next) {
41 | res.status(err.status || 500);
42 | res.render('error', {
43 | message: err.message,
44 | error: err
45 | });
46 | });
47 | }
48 |
49 | // production error handler
50 | // no stacktraces leaked to user
51 | app.use(function(err, req, res, next) {
52 | res.status(err.status || 500);
53 | res.render('error', {
54 | message: err.message,
55 | error: {}
56 | });
57 | });
58 |
59 |
60 | module.exports = app;
61 |
--------------------------------------------------------------------------------
/06-handlebars/bin/www:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env node
2 |
3 | /**
4 | * Module dependencies.
5 | */
6 |
7 | var app = require('../app');
8 | var debug = require('debug')('05-express-first-app:server');
9 | var http = require('http');
10 |
11 | /**
12 | * Get port from environment and store in Express.
13 | */
14 |
15 | var port = normalizePort(process.env.PORT || '8000');
16 | app.set('port', port);
17 |
18 | /**
19 | * Create HTTP server.
20 | */
21 |
22 | var server = http.createServer(app);
23 |
24 | /**
25 | * Listen on provided port, on all network interfaces.
26 | */
27 |
28 | server.listen(port);
29 | server.on('error', onError);
30 | server.on('listening', onListening);
31 |
32 | /**
33 | * Normalize a port into a number, string, or false.
34 | */
35 |
36 | function normalizePort(val) {
37 | var port = parseInt(val, 10);
38 |
39 | if (isNaN(port)) {
40 | // named pipe
41 | return val;
42 | }
43 |
44 | if (port >= 0) {
45 | // port number
46 | return port;
47 | }
48 |
49 | return false;
50 | }
51 |
52 | /**
53 | * Event listener for HTTP server "error" event.
54 | */
55 |
56 | function onError(error) {
57 | if (error.syscall !== 'listen') {
58 | throw error;
59 | }
60 |
61 | var bind = typeof port === 'string'
62 | ? 'Pipe ' + port
63 | : 'Port ' + port;
64 |
65 | // handle specific listen errors with friendly messages
66 | switch (error.code) {
67 | case 'EACCES':
68 | console.error(bind + ' requires elevated privileges');
69 | process.exit(1);
70 | break;
71 | case 'EADDRINUSE':
72 | console.error(bind + ' is already in use');
73 | process.exit(1);
74 | break;
75 | default:
76 | throw error;
77 | }
78 | }
79 |
80 | /**
81 | * Event listener for HTTP server "listening" event.
82 | */
83 |
84 | function onListening() {
85 | var addr = server.address();
86 | var bind = typeof addr === 'string'
87 | ? 'pipe ' + addr
88 | : 'port ' + addr.port;
89 | debug('Listening on ' + bind);
90 | }
91 |
--------------------------------------------------------------------------------
/06-handlebars/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "05-express-first-app",
3 | "version": "0.0.0",
4 | "private": true,
5 | "scripts": {
6 | "start": "node ./bin/www"
7 | },
8 | "dependencies": {
9 | "body-parser": "~1.13.2",
10 | "cookie-parser": "~1.3.5",
11 | "debug": "~2.2.0",
12 | "express": "~4.13.1",
13 | "express-handlebars": "^3.0.0",
14 | "morgan": "~1.6.1",
15 | "serve-favicon": "~2.3.0"
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/06-handlebars/public/stylesheets/style.css:
--------------------------------------------------------------------------------
1 | body {
2 | padding: 50px;
3 | font: 14px "Lucida Grande", Helvetica, Arial, sans-serif;
4 | }
5 |
6 | a {
7 | color: #00B7FF;
8 | }
9 |
--------------------------------------------------------------------------------
/06-handlebars/routes/index.js:
--------------------------------------------------------------------------------
1 | var express = require('express');
2 | var router = express.Router();
3 |
4 | /* GET home page. */
5 | router.get('/', function(req, res, next) {
6 | res.render('index', { title: 'Cool, huh!', condition: true, anyArray: [1,2,3] });
7 | });
8 |
9 | module.exports = router;
10 |
--------------------------------------------------------------------------------
/06-handlebars/views/error.hbs:
--------------------------------------------------------------------------------
1 | {{ message }}
--------------------------------------------------------------------------------
/06-handlebars/views/index.hbs:
--------------------------------------------------------------------------------
1 | {{ title }}
2 | Welcome to {{ title }}
3 | {{# if condition }}
4 | Condition is met
5 | {{ else }}
6 | Condition not met
7 | {{/if}}
8 |
9 | {{# each anyArray as |val key|}}
10 | {{ key }}: {{ val }}
11 | {{/each}}
--------------------------------------------------------------------------------
/06-handlebars/views/layouts/layout.hbs:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | {{ title }}
6 |
7 |
8 |
9 | {{{ body }}}
10 |
11 |
12 |
--------------------------------------------------------------------------------
/07-get-post/app.js:
--------------------------------------------------------------------------------
1 | var express = require('express');
2 | var path = require('path');
3 | var favicon = require('serve-favicon');
4 | var logger = require('morgan');
5 | var cookieParser = require('cookie-parser');
6 | var bodyParser = require('body-parser');
7 | var hbs = require('express-handlebars');
8 |
9 | var routes = require('./routes/index');
10 |
11 | var app = express();
12 |
13 | // view engine setup
14 | app.engine('hbs', hbs({extname: 'hbs', defaultLayout: 'layout', layoutsDir: __dirname + '/views/layouts/'}));
15 | app.set('views', path.join(__dirname, 'views'));
16 | app.set('view engine', 'hbs');
17 |
18 | // uncomment after placing your favicon in /public
19 | //app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
20 | app.use(logger('dev'));
21 | app.use(bodyParser.json());
22 | app.use(bodyParser.urlencoded({ extended: false }));
23 | app.use(cookieParser());
24 | app.use(express.static(path.join(__dirname, 'public')));
25 |
26 | app.use('/', routes);
27 |
28 | // catch 404 and forward to error handler
29 | app.use(function(req, res, next) {
30 | var err = new Error('Not Found');
31 | err.status = 404;
32 | next(err);
33 | });
34 |
35 | // error handlers
36 |
37 | // development error handler
38 | // will print stacktrace
39 | if (app.get('env') === 'development') {
40 | app.use(function(err, req, res, next) {
41 | res.status(err.status || 500);
42 | res.render('error', {
43 | message: err.message,
44 | error: err
45 | });
46 | });
47 | }
48 |
49 | // production error handler
50 | // no stacktraces leaked to user
51 | app.use(function(err, req, res, next) {
52 | res.status(err.status || 500);
53 | res.render('error', {
54 | message: err.message,
55 | error: {}
56 | });
57 | });
58 |
59 |
60 | module.exports = app;
61 |
--------------------------------------------------------------------------------
/07-get-post/bin/www:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env node
2 |
3 | /**
4 | * Module dependencies.
5 | */
6 |
7 | var app = require('../app');
8 | var debug = require('debug')('05-express-first-app:server');
9 | var http = require('http');
10 |
11 | /**
12 | * Get port from environment and store in Express.
13 | */
14 |
15 | var port = normalizePort(process.env.PORT || '8000');
16 | app.set('port', port);
17 |
18 | /**
19 | * Create HTTP server.
20 | */
21 |
22 | var server = http.createServer(app);
23 |
24 | /**
25 | * Listen on provided port, on all network interfaces.
26 | */
27 |
28 | server.listen(port);
29 | server.on('error', onError);
30 | server.on('listening', onListening);
31 |
32 | /**
33 | * Normalize a port into a number, string, or false.
34 | */
35 |
36 | function normalizePort(val) {
37 | var port = parseInt(val, 10);
38 |
39 | if (isNaN(port)) {
40 | // named pipe
41 | return val;
42 | }
43 |
44 | if (port >= 0) {
45 | // port number
46 | return port;
47 | }
48 |
49 | return false;
50 | }
51 |
52 | /**
53 | * Event listener for HTTP server "error" event.
54 | */
55 |
56 | function onError(error) {
57 | if (error.syscall !== 'listen') {
58 | throw error;
59 | }
60 |
61 | var bind = typeof port === 'string'
62 | ? 'Pipe ' + port
63 | : 'Port ' + port;
64 |
65 | // handle specific listen errors with friendly messages
66 | switch (error.code) {
67 | case 'EACCES':
68 | console.error(bind + ' requires elevated privileges');
69 | process.exit(1);
70 | break;
71 | case 'EADDRINUSE':
72 | console.error(bind + ' is already in use');
73 | process.exit(1);
74 | break;
75 | default:
76 | throw error;
77 | }
78 | }
79 |
80 | /**
81 | * Event listener for HTTP server "listening" event.
82 | */
83 |
84 | function onListening() {
85 | var addr = server.address();
86 | var bind = typeof addr === 'string'
87 | ? 'pipe ' + addr
88 | : 'port ' + addr.port;
89 | debug('Listening on ' + bind);
90 | }
91 |
--------------------------------------------------------------------------------
/07-get-post/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "05-express-first-app",
3 | "version": "0.0.0",
4 | "private": true,
5 | "scripts": {
6 | "start": "node ./bin/www"
7 | },
8 | "dependencies": {
9 | "body-parser": "~1.13.2",
10 | "cookie-parser": "~1.3.5",
11 | "debug": "~2.2.0",
12 | "express": "~4.13.1",
13 | "express-handlebars": "^3.0.0",
14 | "morgan": "~1.6.1",
15 | "serve-favicon": "~2.3.0"
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/07-get-post/public/stylesheets/style.css:
--------------------------------------------------------------------------------
1 | body {
2 | padding: 50px;
3 | font: 14px "Lucida Grande", Helvetica, Arial, sans-serif;
4 | }
5 |
6 | a {
7 | color: #00B7FF;
8 | }
9 |
--------------------------------------------------------------------------------
/07-get-post/routes/index.js:
--------------------------------------------------------------------------------
1 | var express = require('express');
2 | var router = express.Router();
3 |
4 | /* GET home page. */
5 | router.get('/', function(req, res, next) {
6 | res.render('index', { title: 'Cool, huh!', condition: true, anyArray: [1,2,3] });
7 | });
8 |
9 | router.get('/test/:id', function(req, res, next) {
10 | res.render('test', {output: req.params.id});
11 | });
12 |
13 | router.post('/test/submit', function(req, res, next) {
14 | res.redirect('/test/...');
15 | });
16 |
17 | module.exports = router;
18 |
--------------------------------------------------------------------------------
/07-get-post/views/error.hbs:
--------------------------------------------------------------------------------
1 | {{ message }}
--------------------------------------------------------------------------------
/07-get-post/views/index.hbs:
--------------------------------------------------------------------------------
1 | {{ title }}
2 | Welcome to {{ title }}
3 |
--------------------------------------------------------------------------------
/07-get-post/views/layouts/layout.hbs:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | {{ title }}
6 |
7 |
8 |
9 | {{{ body }}}
10 |
11 |
12 |
--------------------------------------------------------------------------------
/07-get-post/views/test.hbs:
--------------------------------------------------------------------------------
1 | You accessed this page via a GET request
2 | ID passed: {{ output }}
--------------------------------------------------------------------------------
/08-validation-sessions/.gitignore:
--------------------------------------------------------------------------------
1 | /.idea
2 | /node_modules
--------------------------------------------------------------------------------
/08-validation-sessions/app.js:
--------------------------------------------------------------------------------
1 | var express = require('express');
2 | var path = require('path');
3 | var favicon = require('serve-favicon');
4 | var logger = require('morgan');
5 | var cookieParser = require('cookie-parser');
6 | var bodyParser = require('body-parser');
7 | var hbs = require('express-handlebars');
8 | var expressValidator = require('express-validator');
9 | var expressSession = require('express-session');
10 |
11 | var routes = require('./routes/index');
12 |
13 | var app = express();
14 |
15 | // view engine setup
16 | app.engine('hbs', hbs({extname: 'hbs', defaultLayout: 'layout', layoutsDir: __dirname + '/views/layouts/'}));
17 | app.set('views', path.join(__dirname, 'views'));
18 | app.set('view engine', 'hbs');
19 |
20 | // uncomment after placing your favicon in /public
21 | //app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
22 | app.use(logger('dev'));
23 | app.use(bodyParser.json());
24 | app.use(bodyParser.urlencoded({ extended: false }));
25 | app.use(expressValidator());
26 | app.use(cookieParser());
27 | app.use(express.static(path.join(__dirname, 'public')));
28 | app.use(expressSession({secret: 'max', saveUninitialized: false, resave: false}));
29 |
30 | app.use('/', routes);
31 |
32 | // catch 404 and forward to error handler
33 | app.use(function(req, res, next) {
34 | var err = new Error('Not Found');
35 | err.status = 404;
36 | next(err);
37 | });
38 |
39 | // error handlers
40 |
41 | // development error handler
42 | // will print stacktrace
43 | if (app.get('env') === 'development') {
44 | app.use(function(err, req, res, next) {
45 | res.status(err.status || 500);
46 | res.render('error', {
47 | message: err.message,
48 | error: err
49 | });
50 | });
51 | }
52 |
53 | // production error handler
54 | // no stacktraces leaked to user
55 | app.use(function(err, req, res, next) {
56 | res.status(err.status || 500);
57 | res.render('error', {
58 | message: err.message,
59 | error: {}
60 | });
61 | });
62 |
63 |
64 | module.exports = app;
65 |
--------------------------------------------------------------------------------
/08-validation-sessions/bin/www:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env node
2 |
3 | /**
4 | * Module dependencies.
5 | */
6 |
7 | var app = require('../app');
8 | var debug = require('debug')('05-express-first-app:server');
9 | var http = require('http');
10 |
11 | /**
12 | * Get port from environment and store in Express.
13 | */
14 |
15 | var port = normalizePort(process.env.PORT || '8000');
16 | app.set('port', port);
17 |
18 | /**
19 | * Create HTTP server.
20 | */
21 |
22 | var server = http.createServer(app);
23 |
24 | /**
25 | * Listen on provided port, on all network interfaces.
26 | */
27 |
28 | server.listen(port);
29 | server.on('error', onError);
30 | server.on('listening', onListening);
31 |
32 | /**
33 | * Normalize a port into a number, string, or false.
34 | */
35 |
36 | function normalizePort(val) {
37 | var port = parseInt(val, 10);
38 |
39 | if (isNaN(port)) {
40 | // named pipe
41 | return val;
42 | }
43 |
44 | if (port >= 0) {
45 | // port number
46 | return port;
47 | }
48 |
49 | return false;
50 | }
51 |
52 | /**
53 | * Event listener for HTTP server "error" event.
54 | */
55 |
56 | function onError(error) {
57 | if (error.syscall !== 'listen') {
58 | throw error;
59 | }
60 |
61 | var bind = typeof port === 'string'
62 | ? 'Pipe ' + port
63 | : 'Port ' + port;
64 |
65 | // handle specific listen errors with friendly messages
66 | switch (error.code) {
67 | case 'EACCES':
68 | console.error(bind + ' requires elevated privileges');
69 | process.exit(1);
70 | break;
71 | case 'EADDRINUSE':
72 | console.error(bind + ' is already in use');
73 | process.exit(1);
74 | break;
75 | default:
76 | throw error;
77 | }
78 | }
79 |
80 | /**
81 | * Event listener for HTTP server "listening" event.
82 | */
83 |
84 | function onListening() {
85 | var addr = server.address();
86 | var bind = typeof addr === 'string'
87 | ? 'pipe ' + addr
88 | : 'port ' + addr.port;
89 | debug('Listening on ' + bind);
90 | }
91 |
--------------------------------------------------------------------------------
/08-validation-sessions/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "05-express-first-app",
3 | "version": "0.0.0",
4 | "private": true,
5 | "scripts": {
6 | "start": "node ./bin/www"
7 | },
8 | "dependencies": {
9 | "body-parser": "~1.13.2",
10 | "cookie-parser": "~1.3.5",
11 | "debug": "~2.2.0",
12 | "express": "~4.13.1",
13 | "express-handlebars": "^3.0.0",
14 | "express-session": "^1.13.0",
15 | "express-validator": "^2.20.3",
16 | "morgan": "~1.6.1",
17 | "serve-favicon": "~2.3.0"
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/08-validation-sessions/public/stylesheets/style.css:
--------------------------------------------------------------------------------
1 | body {
2 | padding: 50px;
3 | font: 14px "Roboto", Arial, sans-serif;
4 | }
5 |
6 | a {
7 | color: #00B7FF;
8 | }
9 |
10 | .input label {
11 | display: block;
12 | }
13 |
14 | form button {
15 | font: inherit;
16 | margin: 10px 0;
17 | border-radius: 0;
18 | border: none;
19 | box-shadow: 2px 2px 2px #ccc;
20 | color: white;
21 | background: #3498db;
22 | cursor: pointer;
23 | }
24 |
25 | form button:hover {
26 | background: #2980b9;
27 | }
28 |
29 | .errors {
30 | background: #e79892;
31 | padding: 5px;
32 | margin-bottom: 10px;
33 | min-width: 200px;
34 | display: inline-block;
35 | border: 1px solid #e74c3c;
36 | }
37 |
38 | .errors ul {
39 | margin: 0;
40 | padding: 0;
41 | list-style: none;
42 | }
43 |
44 | .success {
45 | background: #80cca0;
46 | padding: 5px;
47 | }
48 |
--------------------------------------------------------------------------------
/08-validation-sessions/routes/index.js:
--------------------------------------------------------------------------------
1 | var express = require('express');
2 | var router = express.Router();
3 |
4 | /* GET home page. */
5 | router.get('/', function(req, res, next) {
6 | res.render('index', { title: 'Form Validation', success: req.session.success, errors: req.session.errors });
7 | req.session.errors = null;
8 | });
9 |
10 | router.post('/submit', function(req, res, next) {
11 | req.check('email', 'Invalid email address').isEmail();
12 | req.check('password', 'Password is invalid').isLength({min: 4}).equals(req.body.confirmPassword);
13 |
14 | var errors = req.validationErrors();
15 | if (errors) {
16 | req.session.errors = errors;
17 | req.session.success = false;
18 | } else {
19 | req.session.success = true;
20 | }
21 | res.redirect('/');
22 | });
23 |
24 | module.exports = router;
25 |
--------------------------------------------------------------------------------
/08-validation-sessions/views/error.hbs:
--------------------------------------------------------------------------------
1 | {{ message }}
--------------------------------------------------------------------------------
/08-validation-sessions/views/index.hbs:
--------------------------------------------------------------------------------
1 | {{ title }}
2 | {{# if success }}
3 |
4 | Successful Validation!
5 |
6 | {{ else }}
7 | {{# if errors }}
8 |
9 |
10 | {{# each errors }}
11 | {{ this.msg }}
12 | {{/each}}
13 |
14 |
15 | {{/if}}
16 |
31 | {{/if}}
32 |
--------------------------------------------------------------------------------
/08-validation-sessions/views/layouts/layout.hbs:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | {{ title }}
6 |
7 |
8 |
9 | {{{ body }}}
10 |
11 |
12 |
--------------------------------------------------------------------------------
/08-validation-sessions/views/test.hbs:
--------------------------------------------------------------------------------
1 | You accessed this page via a GET request
2 | ID passed: {{ output }}
--------------------------------------------------------------------------------
/09-mongodb/.gitignore:
--------------------------------------------------------------------------------
1 | /.idea
2 | /node_modules
--------------------------------------------------------------------------------
/09-mongodb/app.js:
--------------------------------------------------------------------------------
1 | var express = require('express');
2 | var path = require('path');
3 | var favicon = require('serve-favicon');
4 | var logger = require('morgan');
5 | var cookieParser = require('cookie-parser');
6 | var bodyParser = require('body-parser');
7 | var hbs = require('express-handlebars');
8 |
9 | var routes = require('./routes/index');
10 |
11 | var app = express();
12 |
13 | // view engine setup
14 | app.engine('hbs', hbs({extname: 'hbs', defaultLayout: 'layout', layoutsDir: __dirname + '/views/layouts/'}));
15 | app.set('views', path.join(__dirname, 'views'));
16 | app.set('view engine', 'hbs');
17 |
18 | // uncomment after placing your favicon in /public
19 | //app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
20 | app.use(logger('dev'));
21 | app.use(bodyParser.json());
22 | app.use(bodyParser.urlencoded({ extended: false }));
23 | app.use(cookieParser());
24 | app.use(express.static(path.join(__dirname, 'public')));
25 |
26 | app.use('/', routes);
27 |
28 | // catch 404 and forward to error handler
29 | app.use(function(req, res, next) {
30 | var err = new Error('Not Found');
31 | err.status = 404;
32 | next(err);
33 | });
34 |
35 | // error handlers
36 |
37 | // development error handler
38 | // will print stacktrace
39 | if (app.get('env') === 'development') {
40 | app.use(function(err, req, res, next) {
41 | res.status(err.status || 500);
42 | res.render('error', {
43 | message: err.message,
44 | error: err
45 | });
46 | });
47 | }
48 |
49 | // production error handler
50 | // no stacktraces leaked to user
51 | app.use(function(err, req, res, next) {
52 | res.status(err.status || 500);
53 | res.render('error', {
54 | message: err.message,
55 | error: {}
56 | });
57 | });
58 |
59 |
60 | module.exports = app;
61 |
--------------------------------------------------------------------------------
/09-mongodb/bin/www:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env node
2 |
3 | /**
4 | * Module dependencies.
5 | */
6 |
7 | var app = require('../app');
8 | var debug = require('debug')('05-express-first-app:server');
9 | var http = require('http');
10 |
11 | /**
12 | * Get port from environment and store in Express.
13 | */
14 |
15 | var port = normalizePort(process.env.PORT || '8000');
16 | app.set('port', port);
17 |
18 | /**
19 | * Create HTTP server.
20 | */
21 |
22 | var server = http.createServer(app);
23 |
24 | /**
25 | * Listen on provided port, on all network interfaces.
26 | */
27 |
28 | server.listen(port);
29 | server.on('error', onError);
30 | server.on('listening', onListening);
31 |
32 | /**
33 | * Normalize a port into a number, string, or false.
34 | */
35 |
36 | function normalizePort(val) {
37 | var port = parseInt(val, 10);
38 |
39 | if (isNaN(port)) {
40 | // named pipe
41 | return val;
42 | }
43 |
44 | if (port >= 0) {
45 | // port number
46 | return port;
47 | }
48 |
49 | return false;
50 | }
51 |
52 | /**
53 | * Event listener for HTTP server "error" event.
54 | */
55 |
56 | function onError(error) {
57 | if (error.syscall !== 'listen') {
58 | throw error;
59 | }
60 |
61 | var bind = typeof port === 'string'
62 | ? 'Pipe ' + port
63 | : 'Port ' + port;
64 |
65 | // handle specific listen errors with friendly messages
66 | switch (error.code) {
67 | case 'EACCES':
68 | console.error(bind + ' requires elevated privileges');
69 | process.exit(1);
70 | break;
71 | case 'EADDRINUSE':
72 | console.error(bind + ' is already in use');
73 | process.exit(1);
74 | break;
75 | default:
76 | throw error;
77 | }
78 | }
79 |
80 | /**
81 | * Event listener for HTTP server "listening" event.
82 | */
83 |
84 | function onListening() {
85 | var addr = server.address();
86 | var bind = typeof addr === 'string'
87 | ? 'pipe ' + addr
88 | : 'port ' + addr.port;
89 | debug('Listening on ' + bind);
90 | }
91 |
--------------------------------------------------------------------------------
/09-mongodb/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "05-express-first-app",
3 | "version": "0.0.0",
4 | "private": true,
5 | "scripts": {
6 | "start": "node ./bin/www"
7 | },
8 | "dependencies": {
9 | "body-parser": "~1.13.2",
10 | "cookie-parser": "~1.3.5",
11 | "debug": "~2.2.0",
12 | "express": "~4.13.1",
13 | "express-handlebars": "^3.0.0",
14 | "mongodb": "^2.1.11",
15 | "morgan": "~1.6.1",
16 | "serve-favicon": "~2.3.0"
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/09-mongodb/public/stylesheets/style.css:
--------------------------------------------------------------------------------
1 | body {
2 | padding: 50px;
3 | font: 14px "Lucida Grande", Helvetica, Arial, sans-serif;
4 | }
5 |
6 | a {
7 | color: #00B7FF;
8 | }
9 |
10 | section {
11 | float: left;
12 | background: #bdc3c7;
13 | padding: 10px;
14 | margin: 30px;
15 | width: 300px;
16 | box-shadow: 3px 3px 1px #34495e;
17 | min-height: 400px;
18 | }
19 |
20 | section:first-of-type {
21 | margin-left: 0;
22 | }
23 |
24 | section:last-of-type {
25 | margin-right: 0;
26 | }
27 |
28 | section h3 {
29 | border-bottom: 1px solid black;
30 | padding-bottom: 5px;
31 | }
32 |
33 | .insert {
34 | background: #2ecc71;
35 | }
36 |
37 | .get {
38 | background: #ecf0f1;
39 | }
40 |
41 | .update {
42 | background: #3498db;
43 | }
44 |
45 | .delete {
46 | background: #e74c3c;
47 | }
48 |
49 | .input label {
50 | display: block;
51 | font-weight: bold;
52 | padding: 2px 0;
53 | }
54 |
55 | input,
56 | button {
57 | font: inherit;
58 | }
59 |
60 | button {
61 | margin-top: 10px;
62 | border: none;
63 | box-shadow: 1px 1px 1px #34495e;
64 | border-radius: 0;
65 | background: #ecf0f1;
66 | cursor: pointer;
67 | }
68 |
69 | button:hover {
70 | background: #bdc3c7;
71 | }
72 |
73 | .item {
74 | margin: 10px 0;
75 | padding: 5px;
76 | background: #95a5a6;
77 | border: 1px solid black;
78 | }
--------------------------------------------------------------------------------
/09-mongodb/routes/index.js:
--------------------------------------------------------------------------------
1 | var express = require('express');
2 | var router = express.Router();
3 | var mongo = require('mongodb').MongoClient;
4 | var objectId = require('mongodb').ObjectID;
5 | var assert = require('assert');
6 |
7 | var url = 'mongodb://localhost:27017/test';
8 |
9 | /* GET home page. */
10 | router.get('/', function(req, res, next) {
11 | res.render('index');
12 | });
13 |
14 | router.get('/get-data', function(req, res, next) {
15 | var resultArray = [];
16 | mongo.connect(url, function(err, db) {
17 | assert.equal(null, err);
18 | var cursor = db.collection('user-data').find();
19 | cursor.forEach(function(doc, err) {
20 | assert.equal(null, err);
21 | resultArray.push(doc);
22 | }, function() {
23 | db.close();
24 | res.render('index', {items: resultArray});
25 | });
26 | });
27 | });
28 |
29 | router.post('/insert', function(req, res, next) {
30 | var item = {
31 | title: req.body.title,
32 | content: req.body.content,
33 | author: req.body.author
34 | };
35 |
36 | mongo.connect(url, function(err, db) {
37 | assert.equal(null, err);
38 | db.collection('user-data').insertOne(item, function(err, result) {
39 | assert.equal(null, err);
40 | console.log('Item inserted');
41 | db.close();
42 | });
43 | });
44 |
45 | res.redirect('/');
46 | });
47 |
48 | router.post('/update', function(req, res, next) {
49 | var item = {
50 | title: req.body.title,
51 | content: req.body.content,
52 | author: req.body.author
53 | };
54 | var id = req.body.id;
55 |
56 | mongo.connect(url, function(err, db) {
57 | assert.equal(null, err);
58 | db.collection('user-data').updateOne({"_id": objectId(id)}, {$set: item}, function(err, result) {
59 | assert.equal(null, err);
60 | console.log('Item updated');
61 | db.close();
62 | });
63 | });
64 | });
65 |
66 | router.post('/delete', function(req, res, next) {
67 | var id = req.body.id;
68 |
69 | mongo.connect(url, function(err, db) {
70 | assert.equal(null, err);
71 | db.collection('user-data').deleteOne({"_id": objectId(id)}, function(err, result) {
72 | assert.equal(null, err);
73 | console.log('Item deleted');
74 | db.close();
75 | });
76 | });
77 | });
78 |
79 | module.exports = router;
80 |
--------------------------------------------------------------------------------
/09-mongodb/views/error.hbs:
--------------------------------------------------------------------------------
1 | {{ message }}
--------------------------------------------------------------------------------
/09-mongodb/views/index.hbs:
--------------------------------------------------------------------------------
1 | MONGODB - EXERCISE
2 |
20 |
21 | Get Data
22 | LOAD DATA
23 |
24 | {{# each items }}
25 |
26 | Title: {{ this.title }}
27 | Content: {{ this.content }}
28 | Author: {{ this.author }}
29 | ID: {{ this._id }}
30 |
31 | {{/each}}
32 |
33 |
34 |
35 | Update Data
36 |
55 |
56 |
57 |
58 | Delete Data
59 |
66 |
--------------------------------------------------------------------------------
/09-mongodb/views/layouts/layout.hbs:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | {{ title }}
6 |
7 |
8 |
9 | {{{ body }}}
10 |
11 |
12 |
--------------------------------------------------------------------------------
/10-monk/.gitignore:
--------------------------------------------------------------------------------
1 | /.idea
2 | /node_modules
--------------------------------------------------------------------------------
/10-monk/app.js:
--------------------------------------------------------------------------------
1 | var express = require('express');
2 | var path = require('path');
3 | var favicon = require('serve-favicon');
4 | var logger = require('morgan');
5 | var cookieParser = require('cookie-parser');
6 | var bodyParser = require('body-parser');
7 | var hbs = require('express-handlebars');
8 |
9 | var routes = require('./routes/index');
10 |
11 | var app = express();
12 |
13 | // view engine setup
14 | app.engine('hbs', hbs({extname: 'hbs', defaultLayout: 'layout', layoutsDir: __dirname + '/views/layouts/'}));
15 | app.set('views', path.join(__dirname, 'views'));
16 | app.set('view engine', 'hbs');
17 |
18 | // uncomment after placing your favicon in /public
19 | //app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
20 | app.use(logger('dev'));
21 | app.use(bodyParser.json());
22 | app.use(bodyParser.urlencoded({ extended: false }));
23 | app.use(cookieParser());
24 | app.use(express.static(path.join(__dirname, 'public')));
25 |
26 | app.use('/', routes);
27 |
28 | // catch 404 and forward to error handler
29 | app.use(function(req, res, next) {
30 | var err = new Error('Not Found');
31 | err.status = 404;
32 | next(err);
33 | });
34 |
35 | // error handlers
36 |
37 | // development error handler
38 | // will print stacktrace
39 | if (app.get('env') === 'development') {
40 | app.use(function(err, req, res, next) {
41 | res.status(err.status || 500);
42 | res.render('error', {
43 | message: err.message,
44 | error: err
45 | });
46 | });
47 | }
48 |
49 | // production error handler
50 | // no stacktraces leaked to user
51 | app.use(function(err, req, res, next) {
52 | res.status(err.status || 500);
53 | res.render('error', {
54 | message: err.message,
55 | error: {}
56 | });
57 | });
58 |
59 |
60 | module.exports = app;
61 |
--------------------------------------------------------------------------------
/10-monk/bin/www:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env node
2 |
3 | /**
4 | * Module dependencies.
5 | */
6 |
7 | var app = require('../app');
8 | var debug = require('debug')('05-express-first-app:server');
9 | var http = require('http');
10 |
11 | /**
12 | * Get port from environment and store in Express.
13 | */
14 |
15 | var port = normalizePort(process.env.PORT || '8000');
16 | app.set('port', port);
17 |
18 | /**
19 | * Create HTTP server.
20 | */
21 |
22 | var server = http.createServer(app);
23 |
24 | /**
25 | * Listen on provided port, on all network interfaces.
26 | */
27 |
28 | server.listen(port);
29 | server.on('error', onError);
30 | server.on('listening', onListening);
31 |
32 | /**
33 | * Normalize a port into a number, string, or false.
34 | */
35 |
36 | function normalizePort(val) {
37 | var port = parseInt(val, 10);
38 |
39 | if (isNaN(port)) {
40 | // named pipe
41 | return val;
42 | }
43 |
44 | if (port >= 0) {
45 | // port number
46 | return port;
47 | }
48 |
49 | return false;
50 | }
51 |
52 | /**
53 | * Event listener for HTTP server "error" event.
54 | */
55 |
56 | function onError(error) {
57 | if (error.syscall !== 'listen') {
58 | throw error;
59 | }
60 |
61 | var bind = typeof port === 'string'
62 | ? 'Pipe ' + port
63 | : 'Port ' + port;
64 |
65 | // handle specific listen errors with friendly messages
66 | switch (error.code) {
67 | case 'EACCES':
68 | console.error(bind + ' requires elevated privileges');
69 | process.exit(1);
70 | break;
71 | case 'EADDRINUSE':
72 | console.error(bind + ' is already in use');
73 | process.exit(1);
74 | break;
75 | default:
76 | throw error;
77 | }
78 | }
79 |
80 | /**
81 | * Event listener for HTTP server "listening" event.
82 | */
83 |
84 | function onListening() {
85 | var addr = server.address();
86 | var bind = typeof addr === 'string'
87 | ? 'pipe ' + addr
88 | : 'port ' + addr.port;
89 | debug('Listening on ' + bind);
90 | }
91 |
--------------------------------------------------------------------------------
/10-monk/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "05-express-first-app",
3 | "version": "0.0.0",
4 | "private": true,
5 | "scripts": {
6 | "start": "node ./bin/www"
7 | },
8 | "dependencies": {
9 | "body-parser": "~1.13.2",
10 | "cookie-parser": "~1.3.5",
11 | "debug": "~2.2.0",
12 | "express": "~4.13.1",
13 | "express-handlebars": "^3.0.0",
14 | "mongodb": "^1.4.1",
15 | "monk": "^1.0.1",
16 | "morgan": "~1.6.1",
17 | "serve-favicon": "~2.3.0"
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/10-monk/public/stylesheets/style.css:
--------------------------------------------------------------------------------
1 | body {
2 | padding: 50px;
3 | font: 14px "Lucida Grande", Helvetica, Arial, sans-serif;
4 | }
5 |
6 | a {
7 | color: #00B7FF;
8 | }
9 |
10 | section {
11 | float: left;
12 | background: #bdc3c7;
13 | padding: 10px;
14 | margin: 30px;
15 | width: 300px;
16 | box-shadow: 3px 3px 1px #34495e;
17 | min-height: 400px;
18 | }
19 |
20 | section:first-of-type {
21 | margin-left: 0;
22 | }
23 |
24 | section:last-of-type {
25 | margin-right: 0;
26 | }
27 |
28 | section h3 {
29 | border-bottom: 1px solid black;
30 | padding-bottom: 5px;
31 | }
32 |
33 | .insert {
34 | background: #2ecc71;
35 | }
36 |
37 | .get {
38 | background: #ecf0f1;
39 | }
40 |
41 | .update {
42 | background: #3498db;
43 | }
44 |
45 | .delete {
46 | background: #e74c3c;
47 | }
48 |
49 | .input label {
50 | display: block;
51 | font-weight: bold;
52 | padding: 2px 0;
53 | }
54 |
55 | input,
56 | button {
57 | font: inherit;
58 | }
59 |
60 | button {
61 | margin-top: 10px;
62 | border: none;
63 | box-shadow: 1px 1px 1px #34495e;
64 | border-radius: 0;
65 | background: #ecf0f1;
66 | cursor: pointer;
67 | }
68 |
69 | button:hover {
70 | background: #bdc3c7;
71 | }
72 |
73 | .item {
74 | margin: 10px 0;
75 | padding: 5px;
76 | background: #95a5a6;
77 | border: 1px solid black;
78 | }
--------------------------------------------------------------------------------
/10-monk/routes/index.js:
--------------------------------------------------------------------------------
1 | var express = require('express');
2 | var router = express.Router();
3 | var db = require('monk')('localhost:27017/test');
4 | var userData = db.get('user-data');
5 |
6 | /* GET home page. */
7 | router.get('/', function(req, res, next) {
8 | res.render('index');
9 | });
10 |
11 | router.get('/get-data', function(req, res, next) {
12 | var data = userData.find({});
13 | data.on('success', function(docs) {
14 | res.render('index', {items: docs});
15 | });
16 | });
17 |
18 | router.post('/insert', function(req, res, next) {
19 | var item = {
20 | title: req.body.title,
21 | content: req.body.content,
22 | author: req.body.author
23 | };
24 |
25 | userData.insert(item);
26 |
27 | res.redirect('/');
28 | });
29 |
30 | router.post('/update', function(req, res, next) {
31 | var item = {
32 | title: req.body.title,
33 | content: req.body.content,
34 | author: req.body.author
35 | };
36 | var id = req.body.id;
37 |
38 | // userData.update({"_id": db.id(id)}, item);
39 | userData.updateById(id, item);
40 | });
41 |
42 | router.post('/delete', function(req, res, next) {
43 | var id = req.body.id;
44 |
45 | // userData.remove({"_id": db.id(id)});
46 | userData.removeById(id);
47 | });
48 |
49 | module.exports = router;
50 |
--------------------------------------------------------------------------------
/10-monk/views/error.hbs:
--------------------------------------------------------------------------------
1 | {{ message }}
--------------------------------------------------------------------------------
/10-monk/views/index.hbs:
--------------------------------------------------------------------------------
1 | MONGODB - EXERCISE
2 |
20 |
21 | Get Data
22 | LOAD DATA
23 |
24 | {{# each items }}
25 |
26 | Title: {{ this.title }}
27 | Content: {{ this.content }}
28 | Author: {{ this.author }}
29 | ID: {{ this._id }}
30 |
31 | {{/each}}
32 |
33 |
34 |
35 | Update Data
36 |
55 |
56 |
57 |
58 | Delete Data
59 |
66 |
--------------------------------------------------------------------------------
/10-monk/views/layouts/layout.hbs:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | {{ title }}
6 |
7 |
8 |
9 | {{{ body }}}
10 |
11 |
12 |
--------------------------------------------------------------------------------
/11-mongoose/.gitignore:
--------------------------------------------------------------------------------
1 | /.idea
2 | /node_modules
--------------------------------------------------------------------------------
/11-mongoose/app.js:
--------------------------------------------------------------------------------
1 | var express = require('express');
2 | var path = require('path');
3 | var favicon = require('serve-favicon');
4 | var logger = require('morgan');
5 | var cookieParser = require('cookie-parser');
6 | var bodyParser = require('body-parser');
7 | var hbs = require('express-handlebars');
8 |
9 | var routes = require('./routes/index');
10 |
11 | var app = express();
12 |
13 | // view engine setup
14 | app.engine('hbs', hbs({extname: 'hbs', defaultLayout: 'layout', layoutsDir: __dirname + '/views/layouts/'}));
15 | app.set('views', path.join(__dirname, 'views'));
16 | app.set('view engine', 'hbs');
17 |
18 | // uncomment after placing your favicon in /public
19 | //app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
20 | app.use(logger('dev'));
21 | app.use(bodyParser.json());
22 | app.use(bodyParser.urlencoded({ extended: false }));
23 | app.use(cookieParser());
24 | app.use(express.static(path.join(__dirname, 'public')));
25 |
26 | app.use('/', routes);
27 |
28 | // catch 404 and forward to error handler
29 | app.use(function(req, res, next) {
30 | var err = new Error('Not Found');
31 | err.status = 404;
32 | next(err);
33 | });
34 |
35 | // error handlers
36 |
37 | // development error handler
38 | // will print stacktrace
39 | if (app.get('env') === 'development') {
40 | app.use(function(err, req, res, next) {
41 | res.status(err.status || 500);
42 | res.render('error', {
43 | message: err.message,
44 | error: err
45 | });
46 | });
47 | }
48 |
49 | // production error handler
50 | // no stacktraces leaked to user
51 | app.use(function(err, req, res, next) {
52 | res.status(err.status || 500);
53 | res.render('error', {
54 | message: err.message,
55 | error: {}
56 | });
57 | });
58 |
59 |
60 | module.exports = app;
61 |
--------------------------------------------------------------------------------
/11-mongoose/bin/www:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env node
2 |
3 | /**
4 | * Module dependencies.
5 | */
6 |
7 | var app = require('../app');
8 | var debug = require('debug')('05-express-first-app:server');
9 | var http = require('http');
10 |
11 | /**
12 | * Get port from environment and store in Express.
13 | */
14 |
15 | var port = normalizePort(process.env.PORT || '8000');
16 | app.set('port', port);
17 |
18 | /**
19 | * Create HTTP server.
20 | */
21 |
22 | var server = http.createServer(app);
23 |
24 | /**
25 | * Listen on provided port, on all network interfaces.
26 | */
27 |
28 | server.listen(port);
29 | server.on('error', onError);
30 | server.on('listening', onListening);
31 |
32 | /**
33 | * Normalize a port into a number, string, or false.
34 | */
35 |
36 | function normalizePort(val) {
37 | var port = parseInt(val, 10);
38 |
39 | if (isNaN(port)) {
40 | // named pipe
41 | return val;
42 | }
43 |
44 | if (port >= 0) {
45 | // port number
46 | return port;
47 | }
48 |
49 | return false;
50 | }
51 |
52 | /**
53 | * Event listener for HTTP server "error" event.
54 | */
55 |
56 | function onError(error) {
57 | if (error.syscall !== 'listen') {
58 | throw error;
59 | }
60 |
61 | var bind = typeof port === 'string'
62 | ? 'Pipe ' + port
63 | : 'Port ' + port;
64 |
65 | // handle specific listen errors with friendly messages
66 | switch (error.code) {
67 | case 'EACCES':
68 | console.error(bind + ' requires elevated privileges');
69 | process.exit(1);
70 | break;
71 | case 'EADDRINUSE':
72 | console.error(bind + ' is already in use');
73 | process.exit(1);
74 | break;
75 | default:
76 | throw error;
77 | }
78 | }
79 |
80 | /**
81 | * Event listener for HTTP server "listening" event.
82 | */
83 |
84 | function onListening() {
85 | var addr = server.address();
86 | var bind = typeof addr === 'string'
87 | ? 'pipe ' + addr
88 | : 'port ' + addr.port;
89 | debug('Listening on ' + bind);
90 | }
91 |
--------------------------------------------------------------------------------
/11-mongoose/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "05-express-first-app",
3 | "version": "0.0.0",
4 | "private": true,
5 | "scripts": {
6 | "start": "node ./bin/www"
7 | },
8 | "dependencies": {
9 | "body-parser": "~1.13.2",
10 | "cookie-parser": "~1.3.5",
11 | "debug": "~2.2.0",
12 | "express": "~4.13.1",
13 | "express-handlebars": "^3.0.0",
14 | "mongoose": "^4.4.11",
15 | "morgan": "~1.6.1",
16 | "serve-favicon": "~2.3.0"
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/11-mongoose/public/stylesheets/style.css:
--------------------------------------------------------------------------------
1 | body {
2 | padding: 50px;
3 | font: 14px "Lucida Grande", Helvetica, Arial, sans-serif;
4 | }
5 |
6 | a {
7 | color: #00B7FF;
8 | }
9 |
10 | section {
11 | float: left;
12 | background: #bdc3c7;
13 | padding: 10px;
14 | margin: 30px;
15 | width: 300px;
16 | box-shadow: 3px 3px 1px #34495e;
17 | min-height: 400px;
18 | }
19 |
20 | section:first-of-type {
21 | margin-left: 0;
22 | }
23 |
24 | section:last-of-type {
25 | margin-right: 0;
26 | }
27 |
28 | section h3 {
29 | border-bottom: 1px solid black;
30 | padding-bottom: 5px;
31 | }
32 |
33 | .insert {
34 | background: #2ecc71;
35 | }
36 |
37 | .get {
38 | background: #ecf0f1;
39 | }
40 |
41 | .update {
42 | background: #3498db;
43 | }
44 |
45 | .delete {
46 | background: #e74c3c;
47 | }
48 |
49 | .input label {
50 | display: block;
51 | font-weight: bold;
52 | padding: 2px 0;
53 | }
54 |
55 | input,
56 | button {
57 | font: inherit;
58 | }
59 |
60 | button {
61 | margin-top: 10px;
62 | border: none;
63 | box-shadow: 1px 1px 1px #34495e;
64 | border-radius: 0;
65 | background: #ecf0f1;
66 | cursor: pointer;
67 | }
68 |
69 | button:hover {
70 | background: #bdc3c7;
71 | }
72 |
73 | .item {
74 | margin: 10px 0;
75 | padding: 5px;
76 | background: #95a5a6;
77 | border: 1px solid black;
78 | }
--------------------------------------------------------------------------------
/11-mongoose/routes/index.js:
--------------------------------------------------------------------------------
1 | var express = require('express');
2 | var router = express.Router();
3 | var mongoose = require('mongoose');
4 | mongoose.connect('localhost:27017/test');
5 | var Schema = mongoose.Schema;
6 |
7 | var userDataSchema = new Schema({
8 | title: {type: String, required: true},
9 | content: String,
10 | author: String
11 | }, {collection: 'user-data'});
12 |
13 | var UserData = mongoose.model('UserData', userDataSchema);
14 |
15 | /* GET home page. */
16 | router.get('/', function(req, res, next) {
17 | res.render('index');
18 | });
19 |
20 | router.get('/get-data', function(req, res, next) {
21 | UserData.find()
22 | .then(function(doc) {
23 | res.render('index', {items: doc});
24 | });
25 | });
26 |
27 | router.post('/insert', function(req, res, next) {
28 | var item = {
29 | title: req.body.title,
30 | content: req.body.content,
31 | author: req.body.author
32 | };
33 |
34 | var data = new UserData(item);
35 | data.save();
36 |
37 | res.redirect('/');
38 | });
39 |
40 | router.post('/update', function(req, res, next) {
41 | var id = req.body.id;
42 |
43 | UserData.findById(id, function(err, doc) {
44 | if (err) {
45 | console.error('error, no entry found');
46 | }
47 | doc.title = req.body.title;
48 | doc.content = req.body.content;
49 | doc.author = req.body.author;
50 | doc.save();
51 | })
52 | res.redirect('/');
53 | });
54 |
55 | router.post('/delete', function(req, res, next) {
56 | var id = req.body.id;
57 | UserData.findByIdAndRemove(id).exec();
58 | res.redirect('/');
59 | });
60 |
61 | module.exports = router;
62 |
--------------------------------------------------------------------------------
/11-mongoose/views/error.hbs:
--------------------------------------------------------------------------------
1 | {{ message }}
--------------------------------------------------------------------------------
/11-mongoose/views/index.hbs:
--------------------------------------------------------------------------------
1 | MONGODB - EXERCISE
2 |
20 |
21 | Get Data
22 | LOAD DATA
23 |
24 | {{# each items }}
25 |
26 | Title: {{ this.title }}
27 | Content: {{ this.content }}
28 | Author: {{ this.author }}
29 | ID: {{ this._id }}
30 |
31 | {{/each}}
32 |
33 |
34 |
35 | Update Data
36 |
55 |
56 |
57 |
58 | Delete Data
59 |
66 |
--------------------------------------------------------------------------------
/11-mongoose/views/layouts/layout.hbs:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | {{ title }}
6 |
7 |
8 |
9 | {{{ body }}}
10 |
11 |
12 |
--------------------------------------------------------------------------------
/LICENSE.MD:
--------------------------------------------------------------------------------
1 | The MIT License (MIT)
2 | Copyright (c) <2016
3 |
4 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
5 |
6 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
7 |
8 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
--------------------------------------------------------------------------------
/README.MD:
--------------------------------------------------------------------------------
1 | Repository holding the source code of my node.js series on YouTube.
2 |
--------------------------------------------------------------------------------