-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
82 lines (74 loc) · 2.64 KB
/
app.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
const express = require('express'),
routes = require('./routes'),
user = require('./routes/user'),
article = require('./routes/article'),
http = require('http'),
path = require('path'),
mongoskin = require('mongoskin'),
dbUrl = process.env.MONGOHQ_URL || 'mongodb://@localhost:27017/blog',
db = mongoskin.db(dbUrl, {safe: true}),
collections = {
articles: db.collection('articles'),
users: db.collection('users')
};
const session = require('express-session')
const logger = require('morgan')
const errorHandler = require('errorhandler')
const cookieParser = require('cookie-parser')
const bodyParser = require('body-parser')
const methodOverride = require('method-override')
const app = express();
app.locals.appTitle = 'blog-express';
// app.set('appName','blog-express');
app.use(function(req, res, next) {
if (!collections.articles || ! collections.users) return next(new Error('No collections.'))
req.collections = collections;
return next();
});
app.set('port', process.env.PORT || 3000);
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: true }));
app.use(methodOverride());
app.use(require('stylus').middleware(__dirname + '/public'));
app.use(express.static(path.join(__dirname, 'public')));
if ( app.get('env') == 'development' ) {
app.use(errorHandler());
}
//PAGES&ROUTES
app.get('/', routes.index, function(req, res) {});
app.get('/login', user.login, function(req,res){});
app.post('/login', user.authenticate, function(req, res) {});
app.get('/logout', user.logout, function(req, res) {});
app.get('/admin', article.admin, function(req, res) {});
app.get('/post', article.post, function(req, res) {});
app.post('/post', article.postArticle, function(req, res) {});
app.get('/articles/:slug', article.show, function(req, res) {});
// REST API ROUTES
app.get('/api/articles', list, function(req, res) {})
app.post('/api/articles', add, function(req, res) {});
app.put('/api/articles/:id', article.edit, function(req, res) {});
app.delete('/api/articles/:id', article.del, function(req, res) {});
app.all('*', function(req, res) {
res.sendStatus(404);
})
const server = http.createServer(app);
const boot = function () {
server.listen(app.get('port'), function(){
console.info('Express server listening on port ' + app.get('port'));
});
}
const shutdown = function() {
server.close();
}
if (require.main === module) {
boot();
}
else {
console.info('Running app as a module')
exports.boot = boot;
exports.shutdown = shutdown;
exports.port = app.get('port');
}