Advertisement
Guest User

Untitled

a guest
Apr 20th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.88 KB | None | 0 0
  1. const express = require('express')
  2. const dotenv = require('dotenv')
  3. const bodyParser = require('body-parser')
  4. const cors = require('cors')
  5. const webpush = require('web-push')
  6.  
  7. const app = express()
  8.  
  9. dotenv.config()
  10.  
  11. app.use(cors())
  12. app.use(bodyParser.json())
  13.  
  14. webpush.setVapidDetails(process.env.WEB_PUSH_CONTACT, process.env.PUBLIC_VAPID_KEY, process.env.PRIVATE_VAPID_KEY)
  15.  
  16. app.get('/', (req, res) => {
  17. res.send('Hello world!')
  18. })
  19.  
  20. app.post('/notifications/subscribe', (req, res) => {
  21. const subscription = req.body
  22.  
  23. console.log(subscription)
  24.  
  25. const payload = JSON.stringify({
  26. title: 'Hello!',
  27. body: 'It works.',
  28. })
  29.  
  30. webpush.sendNotification(subscription, payload)
  31. .then(result => console.log(result))
  32. .catch(e => console.log(e.stack))
  33.  
  34. res.status(200).json({'success': true})
  35. });
  36.  
  37. app.listen(9000, () => console.log('The server has been started on the port 9000'))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement