{"size": 1804, "ext": "js", "max_stars_repo_path": "server/server.js", "max_stars_repo_name": "billhanyu/blog", "max_stars_repo_stars_event_min_datetime": "2019-05-26T05:32:29.000Z", "max_stars_repo_stars_event_max_datetime": "2019-05-26T05:32:29.000Z", "max_issues_repo_path": "server/server.js", "max_issues_repo_name": "billhanyu/blog", "max_issues_repo_issues_event_min_datetime": "2018-05-21T20:36:25.000Z", "max_issues_repo_issues_event_max_datetime": "2018-06-01T14:11:58.000Z", "max_forks_repo_path": "server/server.js", "max_forks_repo_name": "billhanyu/blog", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 1, "max_issues_count": 6, "max_forks_count": null, "avg_line_length": 22.835443038, "max_line_length": 79, "alphanum_fraction": 0.6468957871}
const express = require('express');
const bodyParser = require('body-parser');
const cookieParser = require('cookie-parser');
const passport = require('passport');
const cors = require('cors');

const isProduction = process.env.NODE_ENV === 'production';
const isTest = process.env.NODE_ENV === 'test';

// nodemailer init
const nodemailer = isTest ? require('nodemailer-mock') : require('nodemailer');
global.transporter = nodemailer.createTransport({
  sendmail: true,
  newline: 'unix',
  path: '/usr/sbin/sendmail',
});

// mongodb models
require('./models/User');
require('./models/Post');
require('./models/Comment');
require('./models/Image');
require('./passport');

// mongo init
let url = process.env.MONGO_URL || 'mongodb://localhost:27017/blog';
const mongoose = require('mongoose');
mongoose.connect(url);

// express app config
const app = express();

app.use(cors());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(cookieParser());
app.use(passport.initialize());

// api routes
app.use(require('./api'));

// catch 404 and forward to error handler
app.use((req, res, next) => {
  const err = new Error('Not Found');
  err.status = 404;
  next(err);
});

// error handlers for app
// error format: { message } as the body returned
if (!isProduction) {
  app.use((err, req, res, next) => {
    console.log(err.stack);
    res.status(err.status || 500);
    res.json({
      message: err.message,
      error: err,
    });
  });
} else {
  app.use((err, req, res, next) => {
    res.status(err.status || 500);
    res.json({
      message: err.message,
      error: {},
    });
  });
}

const port = 1717;
if (!isTest) {
  app.listen(port, async () => {
    console.log(`Blog app listening on ${port}`);
  });
}

// for testing
module.exports = app;
