Last active
February 3, 2019 00:10
-
-
Save Mcdavid95/d4c2f25ea6f57add4a4812a6a6334203 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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