Clone and edit this document
Runkit
Runkit
home page
user forum
new notebook
clone notebook
download notebook
support & documentation
log in
sign up
new notebook
help & feedback
clone this notebook
download this notebook
Sign In
Sign Up
currency formatter endpoint
node v10.24.1
version:
1.0.0
endpoint
share
tweet
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