-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
76 lines (62 loc) · 1.73 KB
/
server.js
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
75
76
var PORT = normalizePort(process.env.port || '5000');
var http = require('http');
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 passport = require('passport');
require('./models/Posts');
require('./models/Comments');
require('./models/Users');
require('./config/passport');
mongoose.connect('mongodb://localhost/posts');
var routes = require('./routes/index');
var app = express();
app.set('port', PORT);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: false
}));
app.use(cookieParser());
app.use('/public', express.static(path.join(__dirname, 'public')));
app.use('/views', express.static(path.join(__dirname, 'views')));
app.use(passport.initialize());
app.use('/', routes);
var server = http.createServer(app);
server.listen(PORT);
server.on('error', function (error) {
if (error.syscall !== 'listen')
throw error;
switch (error.code) {
case 'EACCES':
console.error(PORT + ' requires elevated privileges');
process.exit(1);
break;
case 'EADDRINUSE':
console.error(PORT + ' is already in use');
process.exit(1);
break;
default:
throw error;
break;
}
});
server.on('listening', function () {
var addr = server.address().address;
var port = server.address().port;
console.log('Listening on %s:%s', addr, port);
});
function normalizePort(port) {
var ret = parseInt(port, 10);
if (isNaN(ret))
return port;
if (ret >= 0)
return ret;
return false;
}