-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
88 lines (75 loc) · 2.36 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
83
84
85
86
87
88
/**
* Module dependencies.
*/
var express = require('express');
var routes = require('./routes');
var user = require('./routes/user');
var http = require('http');
var path = require('path');
var exphbs = require('express3-handlebars');
var mongo = require('mongodb');
var monk = require('monk');
var db = monk('localhost:27017/veedio');
var app = express();
hbs = exphbs.create({
defaultLayout: 'main',
// Specify helpers which are only registered on this instance.
helpers: {
foo: function () { return 'https://www.filepicker.io/api/file/e6Oz8XRvTWCJAUm6Rivd'; }
}
});
// all environments
app.set('port', process.env.PORT || 3000);
app.set('views', path.join(__dirname, 'views'));
app.engine('handlebars', hbs.engine);
app.set('view engine', 'handlebars');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(require('stylus').middleware(path.join(__dirname, 'public')));
app.use(express.static(path.join(__dirname, 'public')));
// development only
if ('development' == app.get('env')) {
app.use(express.errorHandler());
}
app.get('/', function(request, response) {
response.render('home');
});
/*Finds the corresponding ink url in the database and builds
html from handlebars template url*/
app.get('/cdn/:id', function(request, response) {
var collection = db.get('urls');
collection.find({_id : request.params.id}, function(e,docs){
response.render('url', {
helpers: {
foo: function () { return docs[0].inkurl; }
}
});
})
});
//lists all of the collections in the database more for debugging
app.get('/collections',function(req,res){
db.driver.collectionNames(function(e,names){
res.json(names);
})
});
app.get('/urls', function(request, response) {
var collection = db.get('urls');
collection.find({}, function(e,docs){
response.send(docs);
})
});
/*Takes the corresponding value of the url key in the request
and returns a url that the image can be accessed at*/
app.post('/upload', function(req, res) {
var collection = db.get('urls');
collection.insert({inkurl: req.body.url}, function(err,docsInserted){
res.send(docsInserted._id);
});
});
app.get('/users', user.list);
http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});