-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
39 lines (27 loc) · 846 Bytes
/
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
'use strict';
require('dotenv').config();
var express = require('express'),
mongo = require('mongodb').MongoClient;
var app = express();
mongo.connect(process.env['MONGO_URI'], function (err, db) {
if (err) {
throw new Error('Database failed to connect!');
} else {
console.log('MongoDB successfully connected on port 27017.');
}
app.use(express.static('public'));
// Handle route /json
app.get('/json', function (req, res, next) {
db.collection('links').find().toArray(function(err, links) {
if (err) {
next(err);
} else {
console.log(links);
res.json(links);
}
});
});
app.listen(process.env.PORT, function () {
console.log('Listening on port 8080...');
});
});