const path = require('path');
const logger = require('morgan');
const cookieParser = require('cookie-parser');
const bodyParser = require('body-parser');
const compression = require('compression');
const apiClient = require('./middlewares/api_client');
const apiProxy = require('./middlewares/api_proxy');
const config = require('./config/environment');
const i18nMdw = require('./middlewares/i18n');
const defaultContext = require('./middlewares/default_context');
const express = require('express');

module.exports = setup(express());

function setup(app) {
  app.locals.revision = config.revision;
  app.locals.gaAccount = config.gaAccount;
  app.locals.kakaoClientID = config.kakao.clientID;
  app.locals.apiServerUrl = config.apiServerUrl;
  app.locals.thumbnailServerUrl = config.thumbnailServerUrl;
  app.locals.noRedirectPaths = config.noRedirectPaths;

  app.set('env', config.env);
  app.set('port', config.port);
  app.set('trust proxy', true);
  app.set('views', path.resolve(__dirname, 'views'));

  // view engine setup
  require('./config/handlebars')(app);

  if (config.env !== 'production') {
    const webpackConfig = require('../webpack.config');
    const compiler = require('webpack')(webpackConfig);
    const devMiddleware = require('webpack-dev-middleware');
    const hotMiddleware = require('webpack-hot-middleware');

    app.use(devMiddleware(compiler, {
      publicPath: webpackConfig.output.publicPath,
      quiet: false
    }));

    app.use(hotMiddleware(compiler, {
      log(output) {
        console.log(output);
      }
    }));
  }

  app.use(logger(config.env === 'production' ? 'combined' : 'dev'));
  app.use(compression());
  app.use('/assets', [
    express.static(path.resolve(__dirname, '../dist')),
    express.static(path.resolve(__dirname, '../assets')),
    express.static(path.resolve(__dirname, '../client')),
    express.static(path.resolve(__dirname, '../node_modules'))
  ]);
  app.get(/\*.html/, express.static(path.resolve(__dirname, '../assets/html')));
  app.use(cookieParser());
  app.use(apiClient.middleware());
  app.use('/api', apiProxy('/api', config.internalApiServerUrl));
  app.use(bodyParser.json());
  app.use(bodyParser.urlencoded({extended: false}));
  app.use(i18nMdw());
  app.use(defaultContext());

  // register routes
  require('./routes')(app);

  app.use('*', (err, req, res, next) => {
    res.status(err.status);
    res.render('index', {
      layout: false
    });
  });

  app.use('*', (req, res, next) => {
    res.render('index', {
      layout: false
    });
  });

  return app;
}

@@ -982,25 +982,24 @@
 resolve(
-__dirname
+'server'
 , 'views
@@ -1716,31 +1716,17 @@
 resolve(
-__dirname, '../
+'
 dist')),
@@ -1754,39 +1754,25 @@
 ath.resolve(
-__dirname, '../
+'
 assets')),%0A 
@@ -1802,31 +1802,17 @@
 resolve(
-__dirname, '../
+'
 client')
@@ -1846,31 +1846,17 @@
 resolve(
-__dirname, '../
+'
 node_mod
@@ -1922,30 +1922,19 @@
 lve(
-__dirname, '../
+'
 assets
-/
+', '
 html

path.resolve 사용법 정확하게