<commit_before>// var express = require('express');
// app.use(express.json());

var fs   = require('fs');
var url  = require('url');
var http = require('http');
var path = require('path');
var qs = require('querystring');

function mainpage(req, res)
{
    console.log("mainpage()");
    var filename = 'rects_c.html';
    fs.readFile(filename, "binary", function(err, file) {
      if(err) {
        res.writeHead(500, {"Content-Type": "text/plain"});
        res.write(err + "\n");
        res.end();
        return;
      }
      res.writeHead(200);
      res.write(file, "binary");
      res.end();
    });
}

function getdoc(req, res)
{
    console.log("getdoc()");
    var filename = 'dnd.json';
    fs.readFile(filename, "binary", function(err, file) {
      if(err) {
        res.writeHead(500, {"Content-Type": "text/plain"});
        res.write(err + "\n");
        res.end();
        return;
      }
      res.writeHead(200);
      res.write(file, "binary");
      res.end();
    });
}

function savedoc(req, res)
{
    console.log("savedoc()");
    var method = req.method;
    if( method !== 'POST' )
    {
        res.writeHead(405, {"Content-Type": "text/plain"});
        res.write("use POST to save" + "\n");
        res.end();
        return;
    }

    var body = '';
    req.on('data', function(data){
        body += data;
    });
    req.on('end', function(){
        var POST = qs.parse(body);
        // console.log(POST);
        var filedata = POST.file;
        filedata = JSON.stringify(JSON.parse(filedata), null, '  ');
        var filename = "saved.json";
        fs.writeFile(filename, filedata, {}, function(err){
            if( err )
            {
                console.log("error saving:", err);
                res.writeHead(500);
                res.write(JSON.stringify({'success':false, 'error':err}));
                res.end();
            } else {
                res.writeHead(200);
                res.write(JSON.stringify({'success':true}));
                res.end();
            }
        });
    });
}

function dispatch(req, res)
{
    var parts = url.parse(req.url);
    switch(parts.pathname)
    {
        case "/favicon.ico":
            res.end();
            break;
        case "/getdoc":
            getdoc(req, res);
            break;
        case "/savedoc":
            savedoc(req, res);
            break;
        default:
            mainpage(req, res);
            break;
    }
}

http.createServer(function (req, res) {
    dispatch(req, res);
}).listen(1337, '127.0.0.1');
console.log('Server running at http://127.0.0.1:1337/');<commit_msg>switch to express.js<commit_after>#!/usr/local/bin/node

var qs         = require('querystring');
var fs         = require('fs');
// npm install express body-parser
var express    = require('express');
var bodyParser = require('body-parser');

var app = express();
app.use(bodyParser());

app.get('/favicon.ico', function(req, res){res.end();});
app.get('/',         mainpage);
app.get('/getdoc',   getdoc);
app.post('/savedoc', savedoc);

var port = 1338;
app.listen(port);
console.log("listening on ", port);

function mainpage(req, res)
{
    console.log("mainpage()");
    var filename = 'rects_c.html';
    fs.readFile(filename, "binary", function(err, file) {
      if(err) {
        res.writeHead(500, {"Content-Type": "text/plain"});
        res.write(err + "\n");
        res.end();
        return;
      }
      res.writeHead(200);
      res.write(file, "binary");
      res.end();
    });
}

function getdoc(req, res)
{
    console.log("getdoc()");
    var filename = 'dnd.json';
    fs.readFile(filename, "binary", function(err, file) {
      if(err) {
        res.writeHead(500, {"Content-Type": "text/plain"});
        res.write(err + "\n");
        res.end();
        return;
      }
      res.writeHead(200);
      res.write(file, "binary");
      res.end();
    });
}

function savedoc(req, res)
{
    console.log("savedoc()");

    var filedata = req.body.file;
    filedata = JSON.stringify(JSON.parse(filedata), null, '  ');

    var filename = "saved.json";
    fs.writeFile(filename, filedata, {}, function(err){
        if( err )
        {
            console.log("error saving:", err);
            res.writeHead(500);
            res.write(JSON.stringify({'success':false, 'error':err}));
            res.end();
        } else {
            res.writeHead(200);
            res.write(JSON.stringify({'success':true}));
            res.end();
        }
    });
}
{'fraction_non_alphanumeric': 0.18620689655172415, 'fraction_numerical': 0.014015572858731925, 'mean_word_length': 2.0964187327823693, 'pattern_counts': {'":': 8, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6830162', 'n_tokens_mistral': 1508, 'n_tokens_neox': 1442, 'n_words': 272}