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

const {createProxyMiddleware} = require('http-proxy-middleware');
const history = require('connect-history-api-fallback');
var app = express();

//下面两行代码解决刷新404
//app.use(history());
app.use(express.static(__dirname+'/public'));
// app.use(express.static(path.join(__dirname, 'public')));


var param = process.argv.splice(2);
var port = param[0];

// 允许所有域名跨域
app.all('*', function (req, res, next) {
    console.log("=============prod===");
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "Content-Type,Content-Length, Authorization, Accept,X-Requested-With");
    res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
    res.header("X-Powered-By", ' 3.2.1');

    console.log("=============prod123");
    // 跨域请求CORS中的预请求
    if (req.method == "OPTIONS") {
        console.log("=============22222");
         res.send(200); //让options请求快速返回
    } else {
        console.log("=============11111");
        next();
    }
});
app.use('/jfb-recruit',
    createProxyMiddleware({
        target: "http://jfb-recruit",
        changeOrigin: true,
        pathRewrite: {
            "^/jfb-recruit": "/"
        }
    }));


app.listen(port);
