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/5-american-british-translator/routes/api.js

25 lines
778 B
JavaScript

"use strict";
const Translator = require("../components/translator.js");
module.exports = function (app) {
const translator = new Translator();
app.route("/api/translate").post((req, res) => {
if (req.body.locale === undefined || req.body.text === undefined) {
return res.json({ error: "Required field(s) missing" });
}
if (!req.body.text) {
return res.json({ error: "No text to translate" });
}
const locale = req.body.locale;
if (locale === "american-to-british" || locale === "british-to-american") {
return res.json({
translation: translator.translate(req.body.text, locale, true),
text: req.body.text,
});
} else {
return res.json({ error: "Invalid value for locale field" });
}
});
};