currency formatter endpoint

node v10.24.1
version: 1.0.0
endpointsharetweet
This endpoint takes an amount in cents and returns the value formatted in USD.
// express.js' official documentation: https://expressjs.com/en/starter/hello-world.html const express = require("express"); // documentation at https://npm.runkit.com/body-parser const bodyParser = require("body-parser"); // documentation at https://npm.runkit.com/cents-to-currency const centsToCurrency = require("cents-to-currency"); const app = express(); const port = 3000; // automatially parse all POSTed JSON input app.use(bodyParser.json()); app.post('/', (req, res) => { if (req.body && req.body.amount) { // convert (if necessary) the input as a number const amount = Number(req.body.amount); // format and return the value in USD return res.json({ value: centsToCurrency(amount) }); } return res.status(500).json({ error: "Invalid input" }); }); app.listen(port, () => console.log(`Converter app listening on ${port}!`));
Because we're using async/await, we're automatically returning a Promise. You can always explicitly return a promise too.
Loading…

no comments

    sign in to comment