const express = require('express');
const cors = require('cors');
const http = require('http');
const fs = require('fs');
const PORT = 4000;


const app = express();
app.use(cors());

const server = http.createServer(app);

// const app = http.createServer(function (request, response) {
//
// });




app.get('/*', (request, response) => {
    console.log('request starting...', new Date());

    const headers = {
        'Access-Control-Allow-Origin': '*',
        'Access-Control-Allow-Methods': 'OPTIONS, POST, GET',
        'Access-Control-Max-Age': 259200000, // 3000 days
        /** add other headers as per requirement */
    };
    if (request.method === 'OPTIONS') {
        respose.writeHead(204, headers);
        response.end();
        return;
    }

    var filePath = './videos/ipcam' + request.url;
    console.log(filePath);;
    fs.readFile(filePath, function (error, content) {
        response.writeHead(200, { 'Access-Control-Allow-Origin': '*' });
        if (error) {
            if (error.code == 'ENOENT') {
                fs.readFile('./404.html', function (error, content) {
                    response.end(content, 'utf-8');
                });
            }
            else {
                response.writeHead(500);
                response.end('Sorry, check with the site admin for error: ' + error.code + ' ..\n');
                response.end();
            }
        }
        else {
            response.end(content, 'utf-8');
        }
    });
});

server.listen(PORT, () => {
    console.log(`Server listening on PORT ${PORT}`);
});