Created
August 25, 2017 02:57
-
-
Save gdhhdg/db34f98ea081b3849cfa65e5b78c47b4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var express = require('express'); | |
var path = require('path'); | |
var favicon = require('serve-favicon'); | |
var logger = require('morgan'); | |
var cookieParser = require('cookie-parser'); | |
var bodyParser = require('body-parser'); | |
var mongoose = require('mongoose'); | |
var mongodb = require('mongodb'); | |
var index = require('./routes/index'); | |
var users = require('./routes/users'); | |
//mongoose.connection.on('open',()=>{ | |
//console.log('Connected to mongodb.'); | |
//mongoose.connection.db.listCollections().toArray(function (err,names) { | |
//console.log(names); | |
var app = express(); | |
// view engine setup | |
app.use(bodyParser.json()); | |
app.use(bodyParser.urlencoded({ extended: false })); | |
app.set('views', path.join(__dirname, 'views')); | |
app.set('view engine', 'hbs'); | |
// uncomment after placing your favicon in /public | |
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico'))); | |
app.use(logger('dev')); | |
//app.use(cookieParser()); | |
app.use(express.static(path.join(__dirname, 'public'))); | |
app.use('/', index); | |
app.use('/users', users); | |
//mongoose | |
app.use(function(req,res,next){ | |
req.db = db; | |
next(); | |
}); | |
app.get('/book',function (req,res) { | |
book.find({}) | |
.exec(function(err,books){ | |
if(err){ | |
res.send('error has occured'); | |
}else{ | |
console.log(books); | |
res.json(books); | |
} | |
}) | |
}); | |
// catch 404 and forward to error handler | |
app.use(function(req, res, next) { | |
var err = new Error('Not Found'); | |
err.status = 404; | |
next(err); | |
}); | |
// error handler | |
app.use(function(err, req, res, next) { | |
// set locals, only providing error in development | |
res.locals.message = err.message; | |
res.locals.error = req.app.get('env') === 'development' ? err : {}; | |
// render the error page | |
res.status(err.status || 500); | |
res.render('error'); | |
}); | |
var db = 'localhost:27017'; | |
mongoose.Promise = global.Promise; | |
var book = require('./bookmodel.js'); | |
mongoose.createConnection(db, err => { | |
console.log(err || `MongoDB connected at ${db}`); | |
}); | |
module.exports = app; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Created by gunnerhatmaker on 7/31/17. | |
*/ | |
var mongoose = require('mongoose'); | |
var Schema = mongoose.Schema; | |
var bookSchema = new Schema({ | |
title: String, | |
author: String, | |
category: String | |
}); | |
module.exports = mongoose.model('Book',bookSchema); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "untitled", | |
"version": "0.0.0", | |
"private": true, | |
"scripts": { | |
"start": "node ./bin/www" | |
}, | |
"dependencies": { | |
"body-parser": "~1.17.1", | |
"cookie-parser": "~1.4.3", | |
"debug": "~2.6.3", | |
"express": "^4.15.4", | |
"hbs": "~4.0.1", | |
"mongodb": "^2.2.31", | |
"mongoose": "^4.11.8", | |
"morgan": "~1.8.1", | |
"serve-favicon": "~2.4.2" | |
}, | |
"devDependencies": { | |
"nodemon": "^1.11.0" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env node | |
/** | |
* Module dependencies. | |
*/ | |
var app = require('../app'); | |
var debug = require('debug')('untitled:server'); | |
var http = require('http'); | |
/** | |
* Get port from environment and store in Express. | |
*/ | |
var port = normalizePort(process.env.PORT || '3000'); | |
app.set('port', port); | |
/** | |
* Create HTTP server. | |
*/ | |
var server = http.createServer(app); | |
/** | |
* Listen on provided port, on all network interfaces. | |
*/ | |
server.listen(port); | |
server.on('error', onError); | |
server.on('listening', onListening); | |
/** | |
* Normalize a port into a number, string, or false. | |
*/ | |
function normalizePort(val) { | |
var port = parseInt(val, 10); | |
if (isNaN(port)) { | |
// named pipe | |
return val; | |
} | |
if (port >= 0) { | |
// port number | |
return port; | |
} | |
return false; | |
} | |
/** | |
* Event listener for HTTP server "error" event. | |
*/ | |
function onError(error) { | |
if (error.syscall !== 'listen') { | |
throw error; | |
} | |
var bind = typeof port === 'string' | |
? 'Pipe ' + port | |
: 'Port ' + port; | |
// handle specific listen errors with friendly messages | |
switch (error.code) { | |
case 'EACCES': | |
console.error(bind + ' requires elevated privileges'); | |
process.exit(1); | |
break; | |
case 'EADDRINUSE': | |
console.error(bind + ' is already in use'); | |
process.exit(1); | |
break; | |
default: | |
throw error; | |
} | |
} | |
/** | |
* Event listener for HTTP server "listening" event. | |
*/ | |
function onListening() { | |
var addr = server.address(); | |
var bind = typeof addr === 'string' | |
? 'pipe ' + addr | |
: 'port ' + addr.port; | |
debug('Listening on ' + bind); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment