1.0.0 • Published 7 years ago

tiny_test_server v1.0.0

Weekly downloads
2
License
ISC
Repository
-
Last release
7 years ago

tiny_test_server

The tiniest dependency-less http and https server i could come up with.

  • ./https holds crt, csr and key files
  • ./VIEWS holds a simple index.html page

NOTE: using fs.readFile("./VIEWS/index.html" within the server request is not a good idea; but I wanted the page to be up to date on every request and for the server to stay tiny so that's what happened.

This code was written for testing purposes.

Beyond self-education, testing, tomfoolery, silliness, shenanigans and good old messing around. I would advise against the use of this code.

Have Fun! :)

##HTTP

module.exports = function http_server(port_num){
    const fs = require("fs");
    const http = require("http");
    const request_handler = (req, res)=>{
        if(req.url === "/"){
            fs.readFile("./VIEWS/index.html", function(error, pgResp){
                if(error){
                    res.writeHead(404);
                    res.write("contents not found")
                }else{
                    res.writeHead(200, {"Content-Type":"text/html"});
                    res.write(pgResp)
                }
                res.end()
            })
        }else{
            if(req.url.indexOf("") === -1){
                res.end()

            }else{
                res.end()                
            }
            res.end()
        }
    }
    const server = http.createServer(request_handler)
    server.listen(port_num, (err)=>{
        if(err) return console.log("error", err);
        console.log("server listening on", port_num)
    })
}

###HTTPS

you can find the https files in the module's https folder

module.exports = function https_server(port_num){
    const fs = require("fs");
    const https = require("https");
    const httpsOptions = {//httpsconfig
        'cert': fs.readFileSync('./https/server.crt'),
        'key': fs.readFileSync('./https/server.key'),
        'ca': fs.readFileSync('./https/server.csr')
    }    
    const request_handler = (req, res)=>{
        if(req.url === "/"){
            fs.readFile("./VIEWS/index_https.html", function(error, pgResp){
                if(error){
                    res.writeHead(404);
                    res.write("contents not found")
                }else{
                    res.writeHead(200, {"Content-Type":"text/html"});
                    res.write(pgResp)
                }
                res.end()
            })
        }else{
            if(req.url.indexOf("") === -1){
                res.end()

            }else{
                res.end()                
            }
            res.end()
        }
    }
    const server = https.createServer(httpsOptions, request_handler)
    server.listen(port_num, (err)=>{
        if(err) return console.log("error", err);
        console.log("server listening on", port_num)
    })
}