Skip to content

Commit

Permalink
readme update and prune request-promise
Browse files Browse the repository at this point in the history
  • Loading branch information
malipetek authored Apr 22, 2023
1 parent 0912622 commit 2a603b4
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 27 deletions.
18 changes: 13 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
# Shopify Node.js + Express Application
# Partytown.js service worker Server with app proxy + simple reverse proxy

This is a Node.js + Express Application that connects to Shopify. The application will authenticate with a shop, request a permanent access token, and then use that access token to make an API call.
## Partytown
This app serves partytown scripts at `/proxy` path to Shopify. These assets are meant to be loaded to the storefront through app proxy which means they become scripts served at first party.

## Reverse Proxy
Reverse proxy is for adding gzip and stripping headers of resources. However maybe it should be used

## License
[![License: MIT](https://img.shields.io/badge/License-MIT-green.svg)](https://github.com/jaayperez/shopify-node-express-app/blob/master/LICENSE)

This work by [Justin Perez](https://justinperez.com) is licensed under a <a rel="license" href="https://github.com/jaayperez/shopify-node-express-app/blob/master/LICENSE">MIT License Copyright © 2020</a>.
## Deploy on Railway
You can deploy with 1 click here.
You will need env variables handy.

[![Deploy on Railway](https://railway.app/button.svg)](https://railway.app/template/YK6PSO?referralCode=g0Vyyu)

## A bit more explanation
https://www.youtube.com/watch?v=f_n-YPq2ZAg
44 changes: 25 additions & 19 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ const crypto = require('crypto');
const cookie = require('cookie');
const nonce = require('nonce')();
const querystring = require('querystring');
const request = require('request-promise');

const apiKey = process.env.SHOPIFY_API_KEY;
const apiSecret = process.env.SHOPIFY_API_SECRET;
Expand All @@ -24,7 +23,7 @@ app.use((req, res, next) => {
app.use('/proxy', express.static('./static'));

app.get('/', (req, res) => {
res.send('Hello Justin!');
res.send('Your server is up. Add shop parameter to url to start a install on a shop. eg /shopify?shop=xxx.myshopify.com');
});

app.use('/reverse-proxy', async (req, res) => {
Expand Down Expand Up @@ -78,7 +77,7 @@ app.get('/shopify', (req, res) => {
});

// Shopify callback route
app.get('/shopify/callback', (req, res) => {
app.get('/shopify/callback', async (req, res) => {
const { shop, hmac, code, state } = req.query;
const stateCookie = cookie.parse(req.headers.cookie).state;

Expand Down Expand Up @@ -121,26 +120,33 @@ app.get('/shopify/callback', (req, res) => {
code,
};

request.post(accessTokenRequestUrl, { json: accessTokenPayload })
.then((accessTokenResponse) => {
const accessToken = accessTokenResponse.access_token;
// DONE: Use access token to make API call to 'shop' endpoint
const shopRequestUrl = 'https://' + shop + '/admin/api/2020-01/shop.json';
const shopRequestHeaders = {
'X-Shopify-Access-Token': accessToken,
};

request.get(shopRequestUrl, { headers: shopRequestHeaders })
.then((shopResponse) => {
res.status(200).end(shopResponse);
fetch(accessTokenRequestUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(accessTokenPayload),
})
.then((accessTokenResponse) => accessTokenResponse.json())
.then((accessTokenResponse) => {
const accessToken = accessTokenResponse.access_token;
const shopRequestUrl = 'https://' + shop + '/admin/api/2020-01/shop.json';
const shopRequestHeaders = {
'X-Shopify-Access-Token': accessToken,
};

fetch(shopRequestUrl, { headers: shopRequestHeaders })
.then((shopResponse) => shopResponse.json())
.then((shopResponse) => {
res.status(200).end(shopResponse);
})
.catch((error) => {
res.status(error.statusCode).send(error.error.error_description);
});
})
.catch((error) => {
res.status(error.statusCode).send(error.error.error_description);
});
})
.catch((error) => {
res.status(error.statusCode).send(error.error.error_description);
});

} else {
res.status(400).send('Required parameters missing');
Expand Down
4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@
"express": "^4.17.2",
"isomorphic-fetch": "^3.0.0",
"nodemon": "^2.0.16",
"nonce": "^1.0.4",
"request": "^2.88.2",
"request-promise": "^4.2.6"
"nonce": "^1.0.4"
}
}

0 comments on commit 2a603b4

Please sign in to comment.