-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
executable file
·55 lines (44 loc) · 1.3 KB
/
server.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
const express = require('express')
const app = express()
const port = process.env.PORT || 3000
const bodyParser = require('body-parser')
const cors = require('cors')
app.use(bodyParser.urlencoded({ extended: true }))
app.use(bodyParser.json())
app.use(cors())
app.get('/', function (req, res) {
res.redirect('/produto/1321/checkout/6544')
})
app.get('/produto/:productId/checkout/:checkoutId', function (req, res) {
res.sendFile('checkout.html', { root: __dirname })
})
const coupon = {
id: 3,
title: 'black friday',
discount: 35
}
const product = {
id: 1321,
title: 'vestido floral',
price: 100,
image: 'https://res-5.cloudinary.com/enjoei/image/upload/c_fill,fl_lossy.progressive,h_398,q_70,w_375/qzancxcixtocajlrgztv.jpg'
}
const checkout = {
id: 6544,
productId: 1321,
shippingPrice: 20,
availableCoupons: [coupon]
}
app.get('/api/checkouts/:checkoutId', function (req, res) {
checkout.totalPrice = product.price + checkout.shippingPrice
if (parseInt(req.query.couponId, 10) === coupon.id) {
checkout.totalPrice -= coupon.discount
}
res.json({ product: product, checkout: checkout })
})
app.post('/api/checkouts/:checkoutId', function (req, res) {
res.json({ status: 'success' })
})
app.listen(port, function () {
console.log(`Listening on http://localhost:${port}`)
})