This repository has been archived on 2023-11-10. You can view files and clone it, but cannot push or open issues or pull requests.
freecodecamp-projects/7-quality-assurance/1-metric-imp-converter/routes/api.js

22 lines
880 B
JavaScript
Raw Normal View History

'use strict';
const ConvertHandler = require('../controllers/convertHandler.js');
module.exports = function (app) {
const convertHandler = new ConvertHandler();
app.get('/api/convert', (req, res) => {
let error
const input = req.query.input
const initNum = convertHandler.getNum(input)
const initUnit = convertHandler.getUnit(input)
if (initNum === 'invalid number') {error = 'invalid number'}
if (initUnit === 'invalid unit') {if (!error){ error = 'invalid unit'} else {error = 'invalid number and unit'}}
if (error){res.json(error)}
const returnNum = parseFloat(convertHandler.convert(initNum, initUnit).toFixed(5))
const returnUnit = convertHandler.getReturnUnit(initUnit)
const string = convertHandler.getString(initNum, initUnit, returnNum, returnUnit)
res.json({ initNum, initUnit, returnNum, returnUnit, string })
})
};