-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
78 lines (66 loc) · 2.01 KB
/
index.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
var express = require('express');
var path = require('path');
var session = require('express-session');
var MongoStore = require('connect-mongo')(session);
var formidable = require('express-formidable');
var flash = require('connect-flash');
var config = require('config-lite');
var mongoose = require('mongoose');
var fs = require('fs');
var morgan = require('morgan');
var favicon = require('express-favicon');
var app = express();
mongoose.connect(config.mongodb);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.use(express.static(path.join(__dirname, 'public')));
app.use(favicon(path.join(__dirname, '/public/favicon.png')));
app.use(session({
name: config.session.key,
secret: config.session.secret,
cookie: {
maxAge: config.session.maxAge
},
store: new MongoStore({
url: config.mongodb
})
}));
app.use(flash());
app.use(formidable({
uploadDir: path.join(__dirname, 'public/img'),
keepExtensions: true,
}));
app.locals.blog = {
title: 'miniBlog',
description: 'Express + Mongodb + Node.js Learning Sample'
};
app.use(function(req, res, next){
res.locals.user = req.session.user;
res.locals.success = req.flash('success').toString();
res.locals.error = req.flash('error').toString();
next();
});
var accessLogStream = fs.createWriteStream(path.join(__dirname, 'logs', 'access.log'), {flags: 'a'});
app.use(morgan('combined', {stream: accessLogStream}));
// routes
app.get('/', function(req, res){
res.redirect('/posts');
});
app.use('/signup', require('./routes/signup'));
app.use('/signin', require('./routes/signin'));
app.use('/signout', require('./routes/signout'));
app.use('/posts', require('./routes/posts'));
app.use(function(req, res){
if(!res.headersSent){
res.render('404');
}
});
// error page
app.use(function(err, req, res, nex){
res.render('error',{
error: err
});
});
app.listen(config.port, function(){
console.log('Express Sample listening on port ' + config.port);
});