Skip to content

Instantly share code, notes, and snippets.

@stoikerty
Last active April 16, 2018 11:37
Show Gist options
  • Save stoikerty/15efbe51489efc3405a55698574a364a to your computer and use it in GitHub Desktop.
Save stoikerty/15efbe51489efc3405a55698574a364a to your computer and use it in GitHub Desktop.
Products Server
const express = require('express')
const cors = require('cors')
const server = express()
server.use(cors({ origin: true, credentials: true }))
server.use((req, res, next) => {
const productsResponse = {
products: [
{
id: '001',
name: 'Travel Card Holder',
price: 9.25,
},
{
id: '002',
name: 'Personalised Cufflinks',
imageUrl: '/img/002.jpg',
price: 45.0,
},
{
id: '003',
name: 'Kids T-shirt',
imageUrl: '/img/003.jpg',
price: 19.95,
},
{
id: '004',
name: 'Gold Rough Diamond Birthstone Stacking Ring',
imageUrl:
'https://cdn.notonthehighstreet.com/fs/2e/42/7515-74c9-400e-a33a-301d8af272f3/preview_gold-set-of-three-genuine-rough-diamond-stacking-ring.jpg',
price: 216.75,
},
],
}
const newProductsResponse = {
products: productsResponse.products.concat([
{
id: '005',
name: 'Personalised Rose Copper Pitcher Jug',
imageUrl:
'https://cdn.notonthehighstreet.com/fs/8b/51/522e-9611-46bd-adc6-6b80834b5c4d/preview_rose-copper-pitcher-jug.jpg',
price: 9.95,
},
{
id: '006',
name: 'Palazzo Trousers',
imageUrl:
'https://cdn.notonthehighstreet.com/system/product_images/images/001/404/676/preview_palazzo-trousers.jpg',
price: 67,
},
]),
}
if (req.originalUrl === '/products') {
console.log('> Retrieved Products')
res.send(productsResponse)
}
if (req.originalUrl === '/products/new') {
console.log('> Retrieved New Products')
res.send(newProductsResponse)
}
})
server.listen(7002, () => {
console.log('Products Server is running on 7002')
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment