-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
74 lines (58 loc) · 1.82 KB
/
server.js
File metadata and controls
74 lines (58 loc) · 1.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/**
* server.js
*
* @description :: Configures the server and loads middleware and starts listening for requests
* @help :: See (link to doco) Apiary?
*/
// Core Modules
var express = require('express');
var path = require('path');
var favicon = require('static-favicon');
var logger = require('morgan');
var bodyParser = require('body-parser'); // TODO - Replace this
var mongoose = require('mongoose');
// Configure database connection
mongoose.connect('mongodb://localhost:27017/ocr');
mongoose.connection.on('error', function() {
console.error('connection error', arguments);
// TODO - What do we do here? - kill process?
});
mongoose.connection.on('open', function() {
console.log('Connected to the database');
});
// Load Routes
var routes = require('./config/routes');
var app = express();
// view engine setup
app.set('views', path.join(__dirname, 'src/views')); // TODO - Delete
app.set('view engine', 'jade'); // TODO - Delete
// Configure server middleware
app.use(favicon());
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded());
app.use(express.static(path.join(__dirname, 'public'))); // TODO - Delete
// Custom Middleware Libraries
// app.use(authentication.isAuthenticated);
// Configure Routes
app.use('/', routes);
/// catch 404 and forwarding to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
/// error handlers
app.use(function (err, req, res, next) {
res.status(err.status || 500);
res.json({
message: err.message,
error: err.error
});
});
app.set('port', process.env.NODE_PORT || 3000);
// Start listening for requests
app.listen(app.get('port'), function() {
console.log('Express server listening on port ' + app.get('port'));
});
module.exports = app;