-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
85 lines (69 loc) · 2.2 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
77
78
79
80
81
82
83
84
85
if (!process.env.PORT) {
require('dotenv').config()
}
const express = require('express');
const Sequelize = require('sequelize')
const path = require('path');
const favicon = require('serve-favicon');
const logger = require('morgan');
const cookieParser = require('cookie-parser');
const bodyParser = require('body-parser');
const methodOverride = require('method-override')
const index = require('./routes/index');
const pets = require('./routes/cats');
const comments = require('./routes/comments');
const purchases = require('./routes/purchases');
const paginate = require('express-paginate');
const app = express();
//const http = require('http').Server(app);
//const io = require('socket.io')(http)
const { Client } = require('pg');
const client = new Client({
connectionString: process.env.DATABASE_URL,
ssl: true
})
client.connect()
sequelize = new Sequelize("cat-craigslist-development", 'root', '', {
dialect: 'postgres'
});
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');
// use pagination middleware
app.use(paginate.middleware(3, 50));
// override with POST having ?_method=DELETE or ?_method=PUT
app.use(methodOverride('_method'))
// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use('/', index);
app.use('/cats', pets);
app.use('/cats/:catId/comments', comments);
// catch 404 and forward to error handler
app.use((req, res, next) => {
const err = new Error('Not Found');
err.status = 404;
next(err);
});
// error handler
app.use((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 : {};
if(err.status == 404){
res.redirect('/404.html')
} else {
res.redirect('/500.html')
}
});
//io.on('connection', function(socket) {
// console.log('connected')
//})
//http.listen(4000, function(){
// console.log('listening on 4000');
//});
module.exports = app;