SHARE
TWEET

Untitled




Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
- class Backend {
- //do not modify the constructor
- constructor(){
- this.baseUrl = '';
- }
- setBaseUrl(theURL) {
- return this.baseUrl = theURL
- }
- getBaseUrl() {
- return this.baseUrl
- }
- get(theGetQuery) {
- fetch(this.baseUrl + theGetQuery)
- .then(res => res.json())
- }
- post(thePostQuery, thePostArg) {
- fetch(this.baseUrl + thePostQuery, {
- method: "POST",
- headers: {
- "Content-Type": "application/json"
- },
- body: JSON.stringify(thePostArg)
- })
- }
- }
- //sample usage
- const API = new Backend();
- API.setBaseUrl("https://api.learnjavascript.online/demo");
- console.log(API.getBaseUrl());
- API.get("/notifications/new.json")
- .then(data => {
- console.log(data.count);
- });
- API.post("/food.json", {
- food: "Pasta"
- })
- .then(data => {
- console.log(data);
- })
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.