├── .gitignore ├── README.md ├── app.js ├── bin ├── npm-debug.log └── www ├── package.json ├── public └── stylesheets │ └── style.css ├── routes ├── index.js ├── user.js └── users.js └── views ├── error.ejs ├── helloworld.ejs ├── index.ejs ├── newuser.ejs └── userlist.ejs /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules/ 2 | package-lock.json -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Update: this repo is no longer being maintained and I can't guarantee the code will continue to work (although unless major changes happen to Express, it should) 2 | 3 | 4 | 5 | # The Dead-Simple Step-by-Step Guide for Front-End Developers to Getting Up and Running with Node.JS, Express, Jade, and MongoDB 6 | 7 | A tutorial and complete sample project for Front-End developers showing how to get Node, Express and Jade up and running, connected to MongoDB, and reading from / writing to the database. 8 | 9 | *New: Updated for 2019!* 10 | 11 | ## Quickstart 12 | 13 | [Visit the tutorial online](https://closebrace.com/tutorials/2017-03-02/the-dead-simple-step-by-step-guide-for-front-end-developers-to-getting-up-and-running-with-nodejs-express-and-mongodb). The rest of the project is provided just to show the functioning finished results. The tutorial will show you how to build all of this stuff, from downloading Node all the way to the end. 14 | 15 | If you want to run this example code, you will need to do an NPM Install, as the node_modules directory has been removed from this repository. You'll also need to set up a MongoDB database. I highly recommend just going through the tutorial! 16 | 17 | 18 | ## Author 19 | 20 | Christopher Buecheler is a web developer who runs [CloseBrace](https://closebrace.com), a site that provides tutorials and training for full-stack JavaScript developers. Previously he's worked for startups like GameSpy, OkCupid, Crispy Gamer, GoldenSpear, Volt Server, Tizra, and Datarista. You can visit him at [his website](http://cwbuecheler.com). 21 | 22 | 23 | ## Contents 24 | 25 | * /public - static directories suchs as /images 26 | * /routes - route files for tutorial project 27 | * /views - views for tutorial project 28 | * README.md - this file 29 | * app.js - central app file for tutorial project 30 | * package.json - package info for tutorial project 31 | -------------------------------------------------------------------------------- /app.js: -------------------------------------------------------------------------------- 1 | var createError = require('http-errors'); 2 | var express = require('express'); 3 | var path = require('path'); 4 | var cookieParser = require('cookie-parser'); 5 | var logger = require('morgan'); 6 | 7 | // New Code 8 | var monk = require('monk'); 9 | var db = monk('localhost:27017/nodetest1'); 10 | 11 | var indexRouter = require('./routes/index'); 12 | var usersRouter = require('./routes/users'); 13 | 14 | var app = express(); 15 | 16 | // view engine setup 17 | app.set('views', path.join(__dirname, 'views')); 18 | app.set('view engine', 'ejs'); 19 | 20 | app.use(logger('dev')); 21 | app.use(express.json()); 22 | app.use(express.urlencoded({ extended: false })); 23 | app.use(cookieParser()); 24 | app.use(express.static(path.join(__dirname, 'public'))); 25 | 26 | // Make our db accessible to our router 27 | app.use(function(req,res,next){ 28 | req.db = db; 29 | next(); 30 | }); 31 | 32 | app.use('/', indexRouter); 33 | app.use('/users', usersRouter); 34 | 35 | // catch 404 and forward to error handler 36 | app.use(function(req, res, next) { 37 | next(createError(404)); 38 | }); 39 | 40 | // error handler 41 | app.use(function(err, req, res, next) { 42 | // set locals, only providing error in development 43 | res.locals.message = err.message; 44 | res.locals.error = req.app.get('env') === 'development' ? err : {}; 45 | 46 | // render the error page 47 | res.status(err.status || 500); 48 | res.render('error'); 49 | }); 50 | 51 | module.exports = app; 52 | -------------------------------------------------------------------------------- /bin/npm-debug.log: -------------------------------------------------------------------------------- 1 | 0 info it worked if it ends with ok 2 | 1 verbose cli [ 'C:\\Program Files\\nodejs\\\\node.exe', 3 | 1 verbose cli 'C:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js', 4 | 1 verbose cli 'start' ] 5 | 2 info using npm@1.4.3 6 | 3 info using node@v0.10.26 7 | 4 verbose node symlink C:\Program Files\nodejs\\node.exe 8 | 5 error Error: ENOENT, open 'D:\sites\node\newtest\bin\package.json' 9 | 6 error If you need help, you may report this *entire* log, 10 | 6 error including the npm and node versions, at: 11 | 6 error 12 | 7 error System Windows_NT 6.2.9200 13 | 8 error command "C:\\Program Files\\nodejs\\\\node.exe" "C:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js" "start" 14 | 9 error cwd D:\sites\node\newtest\bin 15 | 10 error node -v v0.10.26 16 | 11 error npm -v 1.4.3 17 | 12 error path D:\sites\node\newtest\bin\package.json 18 | 13 error code ENOENT 19 | 14 error errno 34 20 | 15 verbose exit [ 34, true ] 21 | -------------------------------------------------------------------------------- /bin/www: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 3 | /** 4 | * Module dependencies. 5 | */ 6 | 7 | var app = require('../app'); 8 | var debug = require('debug')('nodetest1: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 || '3000'); 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 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "nodetest1", 3 | "version": "0.0.0", 4 | "private": true, 5 | "scripts": { 6 | "start": "node ./bin/www" 7 | }, 8 | "dependencies": { 9 | "cookie-parser": "~1.4.4", 10 | "debug": "~2.6.9", 11 | "ejs": "~2.6.1", 12 | "express": "~4.16.1", 13 | "http-errors": "~1.6.3", 14 | "mongodb": "^3.5.4", 15 | "monk": "^7.1.2", 16 | "morgan": "~1.9.1" 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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: 'Express' }); 7 | }); 8 | 9 | /* GET Hello World page. */ 10 | router.get('/helloworld', function(req, res) { 11 | res.render('helloworld', { title: 'Hello, World!' }); 12 | }); 13 | 14 | /* GET Userlist page. */ 15 | router.get('/userlist', function(req, res) { 16 | var db = req.db; 17 | var collection = db.get('usercollection'); 18 | collection.find({},{},function(e,docs){ 19 | res.render('userlist', { 20 | "userlist" : docs 21 | }); 22 | }); 23 | }); 24 | 25 | /* GET New User page. */ 26 | router.get('/newuser', function(req, res) { 27 | res.render('newuser', { title: 'Add New User' }); 28 | }); 29 | 30 | /* POST to Add User Service */ 31 | router.post('/adduser', function(req, res) { 32 | 33 | // Set our internal DB variable 34 | var db = req.db; 35 | 36 | // Get our form values. These rely on the "name" attributes 37 | var userName = req.body.username; 38 | var userEmail = req.body.useremail; 39 | 40 | // Set our collection 41 | var collection = db.get('usercollection'); 42 | 43 | // Submit to the DB 44 | collection.insert({ 45 | "username" : userName, 46 | "email" : userEmail 47 | }, function (err, doc) { 48 | if (err) { 49 | // If it failed, return error 50 | res.send("There was a problem adding the information to the database."); 51 | } 52 | else { 53 | // And forward to success page 54 | res.redirect("userlist"); 55 | } 56 | }); 57 | }); 58 | 59 | module.exports = router; 60 | -------------------------------------------------------------------------------- /routes/user.js: -------------------------------------------------------------------------------- 1 | 2 | /* 3 | * GET users listing. 4 | */ 5 | 6 | exports.list = function(req, res){ 7 | res.send("respond with a resource"); 8 | }; -------------------------------------------------------------------------------- /routes/users.js: -------------------------------------------------------------------------------- 1 | var express = require('express'); 2 | var router = express.Router(); 3 | 4 | /* GET users listing. */ 5 | router.get('/', function(req, res, next) { 6 | res.send('respond with a resource'); 7 | }); 8 | 9 | module.exports = router; 10 | -------------------------------------------------------------------------------- /views/error.ejs: -------------------------------------------------------------------------------- 1 |

<%= message %>

2 |

<%= error.status %>

3 |
<%= error.stack %>
4 | -------------------------------------------------------------------------------- /views/helloworld.ejs: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | <%= title %> 5 | 6 | 7 | 8 |

<%= title %>

9 |

Hello, World! Welcome to <%= title %>

10 | 11 | 12 | -------------------------------------------------------------------------------- /views/index.ejs: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | <%= title %> 5 | 6 | 7 | 8 |

<%= title %>

9 |

Welcome to <%= title %>

10 | 11 | 12 | -------------------------------------------------------------------------------- /views/newuser.ejs: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Add User 5 | 6 | 7 | 8 |

<%= title %>

9 |
10 | 11 | 12 | 13 |
14 | 15 | 16 | -------------------------------------------------------------------------------- /views/userlist.ejs: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | User List 5 | 6 | 7 | 8 |

User List

9 | 18 | 19 | 20 | --------------------------------------------------------------------------------