text: var http = require('http'),
      path = require('path'),
      methods = require('methods'),
      express = require('express'),
      bodyParser = require('body-parser'),
      session = require('express-session'),
      cors = require('cors'),
      passport = require('passport'),
      errorhandler = require('errorhandler'),
      mongoose = require('mongoose'),
      steam   = require('steam-login'),
      cors = require('cors');   
  
  
  var isProduction = process.env.NODE_ENV === 'production';
  
  // Create global app object
  var app = express();
  
  app.use(cors());
  
  // Normal express config defaults
  app.use(require('morgan')('dev'));
  app.use(bodyParser.urlencoded({ extended: false }));
  app.use(bodyParser.json());
  app.use(cors());
  
  app.use(require('express-session')({ resave: false, saveUninitialized: false, secret: 'a secret' }));
  app.use(steam.middleware({
      realm: 'http://localhost:3000/', 
      verify: 'http://localhost:3000/verify',
      apiKey: "79A96D7FC3B1FE5936D2507F55A6C0B8"}
  ));
  
  app.use(require('method-override')());
  app.use(express.static(__dirname + '/public'));
  
  app.use(session({ secret: 'conduit', cookie: { maxAge: 60000 }, resave: false, saveUninitialized: false  }));
  
  if (!isProduction) {
    app.use(errorhandler());
  }
  
  if(isProduction){
    mongoose.connect(process.env.MONGODB_URI);
  } else {
    mongoose.connect('mongodb://localhost/conduit');
    mongoose.set('debug', true);
  }
  
  require('./models/User');
  require('./models/Article');
  require('./models/Comment');
  require('./config/passport');
  let User = mongoose.model('User');
  let Article = mongoose.model('Article');
  
  let userSeed=new User();
  console.log("test");
  
  
  User.findOne({username:"test"}).then(function(user){
    if(!user){
  
      let userSeed=new User();
  userSeed.username="test";
  userSeed.email="test@gmail.com";
  userSeed.setPassword("11111111");
  userSeed.save(function (err, user){
    if(!err){
      let articleSeed=new Article({
        title:"test",
        decription:"Article for test",
        body:"nothing special",
        tagList:["test"],
        imageURL:"httpL//localhost:3000/null"
      });
      articleSeed.author = user
      articleSeed.save(function(err,article){
        if(!err){
      
        }else{
          console.log(err);
        }
      })
    }else{
      console.log(err);
    }
  });
  console.log("succeed");
    }
  })
  
  
  
  app.get('/', function(req, res) {
      res.send(req.user == null ? 'not logged in' : 'hello ' + req.user.username).end();
  });
   
  app.get('/authenticate', steam.authenticate(), function(req, res) {
      res.setHeader("Access-Control-Allow-Origin", "*"); 
      res.redirect('/');
  });
   
  app.get('/verify', steam.verify(), function(req, res) {
      res.setHeader("Access-Control-Allow-Origin", "*");
      res.setHeader("Origin", "http://localhost:3000");
      console.log(req.user);
      res.redirect("http://localhost:4100/settings").end();
  });
   
  app.get('/logout', steam.enforceLogin('/'), function(req, res) {
      req.logout();
      res.redirect('/');
  });
  
  app.use(require('./routes'));
  
  
  /// catch 404 and forward to error handler
  app.use(function(req, res, next) {
    var err = new Error('Not Found');
    err.status = 404;
    next(err);
  });
  
  /// error handlers
  
  // development error handler
  // will print stacktrace
  if (!isProduction) {
    app.use(function(err, req, res, next) {
      console.log(err.stack);
  
      res.status(err.status || 500);
  
      res.json({'errors': {
        message: err.message,
        error: err
      }});
    });
  }
  
  // production error handler
  // no stacktraces leaked to user
  app.use(function(err, req, res, next) {
    res.status(err.status || 500);
    res.json({'errors': {
      message: err.message,
      error: {}
    }});
  });
  
  // finally, let's start our server...
  var server = app.listen( process.env.PORT || 3000, function(){
    console.log('Listening on port ' + server.address().port);
  });
  