{"size": 1890, "ext": "js", "max_stars_repo_path": "server.js", "max_stars_repo_name": "mydevlocal/Book-Library-API", "max_stars_repo_stars_event_min_datetime": "2019-03-14T06:41:51.000Z", "max_stars_repo_stars_event_max_datetime": "2019-03-15T02:47:54.000Z", "max_issues_repo_path": "server.js", "max_issues_repo_name": "mydevlocal/Book-Library-API", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "server.js", "max_forks_repo_name": "mydevlocal/Book-Library-API", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 2, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 26.25, "max_line_length": 61, "alphanum_fraction": 0.6746031746}
const express = require('express');
const path = require('path');
const helmet = require('helmet');
const cors = require('cors');
const bodyParser = require('body-parser');
const mongoose = require('mongoose');

const User = require('./routes/user_route');
const Author = require('./routes/author_route');
const Category = require('./routes/category_route');
const Book = require('./routes/book_route');
const opts = require('./config/options');

const port = process.env.PORT;
const host = process.env.HOST;

const app = express();

// Set up default mongoose connection
// Get Mongoose to use the global promise library
// Get the default connection
// Bind connection info to log file
mongoose.connect(opts.mongodb.dbURL, opts.mongodb.dbOptions);
mongoose.Promise = global.Promise;
const db = mongoose.connection;
db.on('error', (err) => { throw new Error(err); });

// set up middlewares
app.use(helmet());
app.use(cors({
	origin: true,
	methods: ['PUT', 'GET', 'POST', 'DELETE', 'OPTIONS'],
	allowedHeaders: ['Content-Type', 'Authorization'],
	preflighContinues: true,
	credentials: true,
}));

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));

// set up routes
app.use('/', express.static(path.join(__dirname, 'public')));
app.use('/api/v1', User);
app.use('/api/v1', Author);
app.use('/api/v1', Category);
app.use('/api/v1', Book);

// set up error handling
app.use((req, res, next) => {
	const err = new Error('No routes Found!');
	err.status = 404;
	next(err);
});

if (app.get('env') === 'development') {
	app.use((err, req, res, next) => {
		res.status(err.status || 500);
		res.send({ message: err.message, error: err });
	});
}

app.use((err, req, res, next) => {
	res.status(err.status || 500);
	res.send({ message: err.message, error: {} });
});

// launch the server
app.listen(port, host);

// exporting the app module
module.exports = app;
