Page MenuHomePhabricator
Paste P2712

Code from @Khannaanant262129 in T127329
ActivePublic

Authored by Khannaanant262129 on Mar 6 2016, 3:59 AM.
var http = require('http');
var fs = require('fs');
http.createServer(onRequest).listen(8888);
console.log("server is running....");
content = fs.readFileSync('wiki.html').toString();
replacetags(content);
function replacetags(text){
console.log(text);
// for link tags '<a>' tag to [[ ]]
var linkOpen = text.replace("<a>", "[[");
var linkClose = linkOpen.replace("</a>", "]]");
//for heading tags '<h1'> to '</h1>
var hOpen = linkClose.replace("<h1>" , "=");
var hClose = hOpen.replace("</h1>" , "=");
//for line breaks
var brTagsRemoved = hClose.replace("<br>", "{{break}}");
var removeIOpen = brTagsRemoved.replace("<i>" , "''");
var final = removeIOpen.replace("</i>","''");
console.log(final);
fs.writeFileSync('wikitext.html' , final);
}
//404 response
function send404Response(response){
response.writeHead(404, {"Content-Type" : "text/plain"});
response.write("Error404: Page not found!");
response.end();
}
//Handle request
function onRequest(request,response){
if( request.method == 'GET' && request.url == '/')
{
response.writeHead(200, {"Content-Type" : "text/plain"});
fs.createReadStream("./wikitext.html").pipe(response);
//pipe the page through the response object
}
else {
send404Response(response);
}
}