Code Monkey home page Code Monkey logo

mortgage-calculator's People

Watchers

 avatar

mortgage-calculator's Issues

Code review

  1. I'd prefer to extract port to config
    const server = new Server(process.env.PORT || 8080, database);
  2. You may remove this collection and read it from the disk at server start
    const routing = {
    '/': '/mortgageCalculator.html',
    '/mortgageCalculator': '/mortgageCalculator.html',
    '/bankList': '/public/html/bankList.html',
    }
  3. Rewrite this
    _reqMethods = {
    'GET': (req, res) => this.handleGetRequest(req, res),
    'POST': (req, res) => this.handlePostRequest(req, res),
    'DELETE': (req, res) => this.handleDeleteRequest(req, res),
    }

    to following:
const _reqMethods = {
    GET: handleGetRequest,
    POST: handlePostRequest,
    DELETE: handleDeleteRequest,
 };
  1. You can rewrite
    const extention = name.split('.')[1];
    to
const [, extention] = name.split('.');
  1. Why let and bad naming:
    let name = req.url;

    better: const { url } = req;
  2. Do not rewrite values if possible:
    if (routing[name]) name = routing[name];
  3. When you use code block {} in else branch, you should use block in positive if branch as well
    if (err) console.error('in handle request ' + err);
    else {
    res.writeHead(200, { 'Content-Type': `${typeAns}; charset=utf-8` });
    res.write(data);
    };
  4. get and post handlers contains duplicate code:
    handleGetRequest(req, res) {
    let name = req.url;
    console.log(req.method, name);
    if (routing[name]) name = routing[name];
    if (this._dbRequests[name]) {
    this._dbRequests[name](res);
    return;
    }
    const extention = name.split('.')[1];
    const typeAns = mime[extention];
    fs.readFile('.' + name, (err, data) => {
    if (err) console.error('in handle request ' + err);
    else {
    res.writeHead(200, { 'Content-Type': `${typeAns}; charset=utf-8` });
    res.write(data);
    };
    res.end();
    });
    }
    handlePostRequest(req, res) {
    let name = req.url;
    console.log(req.method, name);
    let data = '';
    req.on('error', (err) => console.error(err));
    req.on('data', chunk => {
    data += chunk;
    });
    req.on('end', async () => {
    const dataParsed = JSON.parse(data);
    let result = null;
    const con = await this.database.createConnection();
    con.connect(async (err) => {
    if (err) throw err;
    if (name === '/newBank') result = (await this.database.putBankInDB(dataParsed)).toString();
    else if(name === '/updateBankInfo') {
    const ans = await this.database.updateBankInfo(dataParsed);
    const obj = this.getResponseObject(ans[0]);
    result = JSON.stringify(obj);
    }
    res.writeHead(200, { 'Content-Type': `${mime['txt']}; charset=utf-8` });
    res.write(result);
    res.end();
    con.destroy();
    });
    });
    }
  5. Instead of this
    let data = '';
    req.on('error', (err) => console.error(err));
    req.on('data', chunk => {
    data += chunk;
    });

    you can use for await: https://github.com/HowProgrammingWorks/DDD/blob/master/JavaScript/2-controller/body.js
  6. Terrible mix of http-aware, db-aware, and domain logic code:
    req.on('end', async () => {
    const dataParsed = JSON.parse(data);
    let result = null;
    const con = await this.database.createConnection();
    con.connect(async (err) => {
    if (err) throw err;
    if (name === '/newBank') result = (await this.database.putBankInDB(dataParsed)).toString();
    else if(name === '/updateBankInfo') {
    const ans = await this.database.updateBankInfo(dataParsed);
    const obj = this.getResponseObject(ans[0]);
    result = JSON.stringify(obj);
    }
    res.writeHead(200, { 'Content-Type': `${mime['txt']}; charset=utf-8` });
    res.write(result);
    res.end();
    con.destroy();
    });
  7. Use for..of and Array.prototype.entries() instead of
    let i = 0;
    for (let bank of banks) {
    const dataI = this.getResponseObject(bank);
    data.push(dataI);
    i++;
    }
  8. Duplicate code :
    let data = '';
    req.on('error', (err) => console.error(err));
    req.on('data', chunk => {
    data += chunk;
    });
  9. SQL-injection:
    const query = `DELETE FROM cwDB.bank WHERE cwDB.bank.bank_id = ${id}`;
  10. Use eslint and prettier for code consistency autoformatting

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.