-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
114 lines (91 loc) · 3.39 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
require('dotenv').config()
const express = require('express');
const axios = require('axios');
const app = express();
app.set('view engine', 'pug');
app.use(express.static(__dirname + '/public'));
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
// * Please DO NOT INCLUDE the private app access token in your repo. Don't do this practicum in your normal account.
const PRIVATE_APP_ACCESS = process.env.API_KEY;
// TODO: ROUTE 1 - Create a new app.get route for the homepage to call your custom object data. Pass this data along to the front-end and create a new pug template in the views folder.
// * Code for Route 1 goes here
app.get('/', async (req, res) => {
const pokemons = 'https://api.hubapi.com/crm/v3/objects/pokemons?properties=name,index,type';
const headers = {
Authorization: `Bearer ${PRIVATE_APP_ACCESS}`,
"Content-Type": "application/json"
}
try {
const response = await axios.get(pokemons, { headers });
const data = response.data.results;
res.render('homepage', { title: "Custom Object Table", data })
} catch (e) {
console.error(e)
}
})
// TODO: ROUTE 2 - Create a new app.get route for the form to create or update new custom object data. Send this data along in the next route.
// * Code for Route 2 goes here
app.get('/update-cobj', async (req, res) => {
res.render('updates', { title: 'Update Custom Object Form | Integrating With HubSpot I Practicum' })
})
// TODO: ROUTE 3 - Create a new app.post route for the custom objects form to create or update your custom object data. Once executed, redirect the user to the homepage.
// * Code for Route 3 goes here
app.post('/update-cobj', async (req, res) => {
const update = {
properties: {
...req.body
}
}
const updateCobj = `https://api.hubapi.com/crm/v3/objects/pokemons`
const headers = {
Authorization: `Bearer ${PRIVATE_APP_ACCESS}`,
"Content-Type": "application/json"
}
try {
await axios.post(updateCobj, update, { headers })
res.redirect('/')
} catch (e) {
console.error(e)
}
})
/**
* * This is sample code to give you a reference for how you should structure your calls.
* * App.get sample
app.get('/contacts', async (req, res) => {
const contacts = 'https://api.hubspot.com/crm/v3/objects/contacts';
const headers = {
Authorization: `Bearer ${PRIVATE_APP_ACCESS}`,
'Content-Type': 'application/json'
}
try {
const resp = await axios.get(contacts, { headers });
const data = resp.data.results;
res.render('contacts', { title: 'Contacts | HubSpot APIs', data });
} catch (error) {
console.error(error);
}
});
* * App.post sample
app.post('/update', async (req, res) => {
const update = {
properties: {
"favorite_book": req.body.newVal
}
}
const email = req.query.email;
const updateContact = `https://api.hubapi.com/crm/v3/objects/contacts/${email}?idProperty=email`;
const headers = {
Authorization: `Bearer ${PRIVATE_APP_ACCESS}`,
'Content-Type': 'application/json'
};
try {
await axios.patch(updateContact, update, { headers } );
res.redirect('back');
} catch(err) {
console.error(err);
}
});
*/
// * Localhost
app.listen(3000, () => console.log('Listening on http://localhost:3000'));