-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
59 lines (46 loc) · 1.63 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
var express = require('express'),
app = express(),
engines = require('consolidate'),
bodyParser = require('body-parser'),
MongoClient = require('mongodb').MongoClient,
assert = require('assert');
app.engine('html', engines.nunjucks);
app.set('view engine', 'html');
app.set('views', __dirname + '/views');
app.use(bodyParser.urlencoded({ extended: true }));
// Handler for internal server errors
function errorHandler(err, req, res, next) {
console.error(err.message);
console.error(err.stack);
res.status(500).render('error_template', { error: err });
}
app.get('/', function(req, res, next) {
res.render('createMovie');
});
app.post('/create_movie', function(req, res, next) {
var url = 'mongodb://localhost:27017/video';
MongoClient.connect(url, function(err, db) {
var title = req.body.title;
var year = req.body.year;
var imdb = req.body.imdb;
if (typeof title == 'undefined') next('Please provide a Title');
else if (typeof year == 'undefined') next('Please provide a Year');
else if (typeof imdb == 'undefined') next('Please provide an IMDB ID');
else {
assert.equal(null, err);
db.collection('movies').insertOne(
{ 'title': title, 'year': year, 'imdb': imdb },
function (err, r) {
assert.equal(null, err);
res.send("Movie inserted with _id: " + r.insertedId);
}
);
}
db.close();
});
});
app.use(errorHandler);
var server = app.listen(3000, function() {
var port = server.address().port;
console.log('Express server listening on port %s.', port);
});