-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
48 lines (40 loc) · 1.07 KB
/
app.js
File metadata and controls
48 lines (40 loc) · 1.07 KB
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
// call in the installed dependencies
const express = require('express'),
bodyParser = require('body-parser'),
mongoose = require('mongoose'),
mainRoutes = require('./routes'),
config = require('./config/config');
require('dotenv').config();
const MONGO_DB_URI = config.mongodbURI;
const port = config.PORT;
const app = express();
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
// connect to mongoose
mongoose.connect(MONGO_DB_URI)
.then(() => {
console.log('database connected.');
})
.catch(() => {
});
mongoose.Promise = global.Promise;
mongoose.set('debug', true);
app.listen(port, (err) => {
if (err) {
console.log(err);
}
console.info('Serving from http://localhost:%s/ in your browser.', port);
});
// set up route
app.use('/api/', mainRoutes);
app.get('/', (req, res) => {
res.status(200).json({
message: 'Welcome to Student CRUD API',
});
});
app.use(function (err, req, res, next) {
if (err.isBoom) {
return res.status(err.output.statusCode).json(err.output.payload);
}
});
module.exports = app;