var createError = require('http-errors');
var express = require('express');

var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');

require('./app/config/common.config');

var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
//app.set('view engine', 'jade');
ejs = require('ejs');
app.engine('.html', ejs.__express);
app.set('view engine', 'html');

//设置静态资源路径
app.use('/cloud',express.static(path.join(__dirname, 'global')));



app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));


var middleWare=require('./app/class/middleWare');
app.use(middleWare.beforeRun);


//路由设置
var router = express.Router();
router.all('*', function(req, res, next) {//设置字符集
  res.header("Content-Type", "application/json;charset=utf-8");
  next();
});

var cloudRouter = require('./app/cloudRouter');
app.use('/', cloudRouter);
var cloudAjaxRouter = require('./app/cloudAjaxRouter');
app.use('/', cloudAjaxRouter);
var contactsAjaxRouter = require('./app/contactsAjaxRouter');
app.use('/', contactsAjaxRouter);
// catch 404 and forward to error handler



//var middleWate=require('./app/class/middleWare');
/*app.use(function(req, res, next) {
  return res.redirect('https://localhost:3000' + req.url);
  /!*console.log('ddd');
  res.location('http://itbilu.com');
  res.statusCode = 302;
  console.log('ddd2');*!/
  //next();
  //middleWate.beforeRun(req, res, next);

});*/


// error handler
/*app.use(function(err, req, res, next) {

  // set locals, only providing error in development
  res.locals.message = err.message;
  console.log(err.message);
  res.locals.error = req.app.get('env') === 'development' ? err : {};

  // render the error page
  res.status(err.status || 500);
  res.render('error');
});*/

module.exports = app;
