-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
58 lines (48 loc) · 1.33 KB
/
app.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
import dotenv from 'dotenv';
import express from 'express';
import fetch from 'node-fetch';
import Shopify from 'shopify-api-node';
dotenv.config();
const app = express();
const shopify = new Shopify({
shopName: process.env.shopName,
apiKey: process.env.apiKey,
password: process.env.password
});
const PORT = 5000;
app.get('/', (req, res) => {
res.send('<h1>Our app is running .....</h1>');
});
async function getProducts() {
const response = await fetch('http://fakestoreapi.com/products');
const data = await response.json();
return data;
}
app.get('/products', async (req, res) => {
try {
const products = await getProducts();
const shopifyProducts = [];
for (const product of products) {
const newProduct = await shopify.product.create({
title: product.title,
body_html: product.description,
variants: [{
price: product.price
}],
images: [{
src: product.image
}],
tags: 'demo-product, test-product'
});
shopifyProducts.push(newProduct);
console.log(newProduct);
}
res.json(shopifyProducts);
} catch (error) {
console.error(error);
res.status(500).send('Error occurred while fetching or creating products.');
}
});
app.listen(PORT, () => {
console.log(`The server is running at ${PORT}....`);
});