-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.js
49 lines (41 loc) · 1.22 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
'use strict';
// Require the modules we need
const express = require('express');
const mongoose = require('mongoose');
const bodyParser = require('body-parser');
const path = require("path");
const logger = require('./logger');
const routes = require('./routes');
require('dotenv').config();
mongoose.connect(process.env.MONGO_CONNECTION_STRING,
{ useMongoClient:true,
promiseLibrary: global.Promise }
);
// Set up express
const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended:true}));
app.use('/public', express.static(path.join(__dirname, 'public')));
app.use(routes)
// Processes homepage request
app.get('/', (req, res) => {
res.sendFile(__dirname + '/views/index.html');
});
// Respond not found to all the wrong routes
app.use((req, res, next) => {
res.status(404);
res.type('txt').send('Not found');
});
// Error Middleware
app.use((err, req, res, next) => {
if(err) {
res.status(err.status || 500)
.type('txt')
.send(err.message || 'SERVER ERROR');
}
});
//Listen for requests
const server = app.listen(process.env.PORT || 3000, () => {
const port = server.address().port;
console.log('Image Search Abstraction Layer app is listening on port ', port);
});