Skip to content

Instantly share code, notes, and snippets.

@Mcdavid95
Last active February 3, 2019 00:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Mcdavid95/d4c2f25ea6f57add4a4812a6a6334203 to your computer and use it in GitHub Desktop.
Save Mcdavid95/d4c2f25ea6f57add4a4812a6a6334203 to your computer and use it in GitHub Desktop.
const express = require('express');
const bodyParser = require('body-parser');
// Our function which adds two numbers and returns the result
const addNumbers = (firstNumber, secondNumber) => {
// check that input is a number
if (typeof(Number(firstNumber)) !== 'number' || typeof(Number(secondNumber)) !== 'number') {
return 'Values should be integer or numbers'
}
return Number(firstNumber) + Number(secondNumber);
}
// Destructure our bodyParser methods
const { urlencoded, json } = bodyParser;
const port = process.env.PORT || 8080;
// intialize our express app
const app = express();
// Parse incoming requests data (https://github.com/expressjs/body-parser)
app.use(json());
app.use(urlencoded({ extended: false }));
// end point to add numbers
app.post('/api/add', (req, res) => {
const { firstNumber, secondNumber } = req.body;
const result = addNumbers(firstNumber, secondNumber);
return res.status(200).send({
result
});
});
// app entry point
app.get('/', (req, res) => res.status(200).send({
message: 'Welcome to our glorious app',
}));
// Setup a default catch-all route that sends back a welcome message in JSON format.
app.get('*', (req, res) => res.status(200).send({
message: 'Welcome to the beginning of nothingness.',
}));
app.listen(port, (err) => {
if (!err) {
console.log(`App started on port ${port}`);
} else {
console.log(err);
}
});
module.exports = app;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment