let http = require('http');
let url = require('url');
let downloadController = require('./controller/downloadController');
let express = require('express');
let bodyParser = require('body-parser')
let app = express();

app.use(bodyParser.json({limit: '50mb'}));
app.use(bodyParser.urlencoded({limit: '50mb', extended: false}));

app.use(function (req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "X-Requested-With");
    res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
    next();
});

app.post('/downloadData', function (req, res) {
    downloadController.start(res,req.body)
})

app.get('/downloadData/process', function (req, res) {
    downloadController.downloadProcess(res, req.query);
})

app.use(`${__dirname}/controller/tmpData`, express.static('tmpData'));

let server = app.listen(8887, function () {
    var host = server.address().address
    var port = server.address().port

    console.log("服务器启动，访问地址为 http://%s:%s", host, port)
})

// function start(route, handle) {
//     function onRequest(request, response) {
//         let pathname = url.parse(request.url).pathname;
//         // console.log(url.parse(request.url));

//         if (request.method === 'GET') {
//             let data = url.parse(request.url).query
//             route(handle, pathname, response, data);
//         }
//         if (request.method === 'POST') {
//             request.on('data', function (data) {
//                 console.log(data);
//                 route(handle, pathname, response, data);
//             })
//         }
//     }
//     console.log('服务器启动')
//     http.createServer(onRequest).listen(8888);
// }

// exports.start = start;