Application Programming Interface Flashcards
API
Interface layer to interact with an external system.
The interactions are via the form of GET and POST requests
Using the Request Module to Get Data from an API
request - NPM module Setup const request = require(“request”); Usage request.(, );
function(error, response, body);
Understanding the JSON format and Working with JSON
JSON
JavaScript Object Notation Used to communicate data back and forth in the web XML [eXtensive Markup Language] Alternative to JSON, came before JSON. Less frequently used now JavaScript object -> string JSON.stringify(); string -> JavaScript Object JSON.parse(); Sending more than one thing res.write(); res.send();
API Calls with Parameters
Please see the following screenshot
Newsletter Walkthrough
Incorporated and modified an existing Bootstrap Sign In Example
In the HTML, some files are directly obtained from the internet via the use of the web urls
However, some are static files such as css and images files.
In order to use them, we must notify Express that they are static
To do this, we can create a folder where all the static files will be contained,
And then call the express.static()
In the form, be sure to specify action and method
So the picture above, the form will POST to the location in action
Posting Data to Mailchimp Servers
Set up MailChimp Account & Obtain API Key
Set up the request url and headers
Authentication
Look through the API documentation on authentication
Adding Success/Failure Pages to the Website
Instead of sending text back upon success or failure. We now send back an individual html file
Redirecting back to a specific page
Method: Create a button, when pressed it will create a post request.
In the server, handle the post request to the location, by redirecting to the intended page
Deploy the project to Heroku and Make it Live
//
What is Heroku?
Heroku allows users to deploy their websites on Heroku’s servers, so that anyone can access the website.
To use Heroku:
- Modify the prior app.listen(3000, ); to app.listen(process.env.PORT || 3000, );
process.env.PORT - Heroku will handle the setting of the port
|| 3000 - This will be used in the case that Heroku is not being used
2. Define a Procfile
Create a file named Procfile that is in the same folder as the main root of the website
Inside, write the same command as you would write in the terminal to execute your website
web: node app.js
3. Initialize and Commit the web project using git
In the web project root directory, execute git init
Add the files, git add .
Commit the files, git commit -m “Commit message”
4. Create a Heroku Reference to your Web Project, Git
Execute, heroku create
By default, heroku creates a random name to your project
But you can pass a parameter to set the name
Execute, git push heroku master
Web project is now deployed on Heroku
5. Running the web site on Heroku
1. Can run heroku open on the root directory of the web project
2. Can run the heroku url given after heroku create
Checking Heroku logs
Run heroku logs