I’ve the below JavaScript
function run perfectly, I used it to run static server without 3rd part library:
const http = require('http');
const url = require('url');
const fs = require('fs');
const path = require('path');
const port = process.argv[2] || 9000;
http.createServer(function (req, res) {
console.log(`${req.method} ${req.url}`);
// parse URL
const parsedUrl = url.parse(req.url);
// extract URL path
let pathname = `.${parsedUrl.pathname}`;
// based on the URL path, extract the file extention. e.g. .js, .doc, ...
const ext = path.parse(pathname).ext;
// maps file extention to MIME typere
const map = {
'.ico': 'image/x-icon',
'.html': 'text/html',
'.js': 'text/javascript',
'.json': 'application/json',
'.css': 'text/css',
'.png': 'image/png',
'.jpg': 'image/jpeg',
'.wav': 'audio/wav',
'.mp3': 'audio/mpeg',
'.svg': 'image/svg+xml',
'.pdf': 'application/pdf',
'.doc': 'application/msword'
};
fs.exists(pathname, function (exist) {
if(!exist) {
// if the file is not found, return 404
res.statusCode = 404;
res.end(`File ${pathname} not found!`);
return;
}
// if is a directory search for index file matching the extention
if (fs.statSync(pathname).isDirectory()) pathname += '/index' + ext;
// read file from file system
fs.readFile(pathname, function(err, data){
if(err){
res.statusCode = 500;
res.end(`Error getting the file: ${err}.`);
} else {
// if the file is found, set Content-type and send data
res.setHeader('Content-type', map[ext] || 'text/plain' );
res.end(data);
}
});
});
}).listen(parseInt(port));
console.log(`Server listening on port ${port}`);
I’m trying to replicate it using Kotlin
, so wrote the below code:
external fun require(module:String):dynamic
fun main(args: Array<String>) {
println("Hello JavaScript!")
val http = require("http")
val url = require("url")
val fs = require("fs")
val path = require("path")
val port = process.argv[2] || 9000
val hostname = "127.0.0.1"
val server = http.createServer{req, res ->
// parse URL
val parsedUrl = url.parse(req.url)
// extract URL path
val pathname = parsedUrl.pathname
// based on the URL path, extract the file extention. e.g. .js, .doc, ...
val ext = path.parse(pathname).ext
// maps file extention to MIME typere
val map = mapOf(
".ico" to "image/x-icon",
".html" to "text/html",
".js" to "text/javascript",
".json" to "application/json",
".css" to "text/css",
".png" to "image/png",
".jpg" to "image/jpeg",
".wav" to "audio/wav",
".mp3" to "audio/mpeg",
".svg" to "image/svg+xml",
".pdf" to "application/pdf",
".doc" to "application/msword"
)
fs.exists(pathname, fun (exist) {
if(!exist) {
// if the file is not found, return 404
res.statusCode = 404;
res.end("File ${pathname} not found!")
}
// if is a directory search for index file matching the extention
if (fs.statSync(pathname).isDirectory()) pathname += "/index" + ext;
// read file from file system
fs.readFile(pathname, fun(err, data){
if(err){
res.statusCode = 500;
res.end("Error getting the file: ${err}.")
} else {
// if the file is found, set Content-type and send data
res.setHeader("Content-type", map.get(ext) || "text/plain" )
res.end(data)
}
})
})
}
server.listen(port, hostname, fun() {
println("Server running at http://${hostname}:${port}/")
})
}
But there are some errors, that I could not fix, which are:
val port = process.argv[2] || 9000 . // process.argv unresolved
fs.exists(pathname, fun (exist) { ..} // exist pls specify it explicitly
fs.readFile(pathname, fun(err, data){ ..} // err and data pls specify it explicitly
res.setHeader("Content-type", map.get(ext) || "text/plain" ) // Required Boolean, found String