Guest User

Untitled

a guest
Jun 22nd, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.01 KB | None | 0 0
  1. const express = require('express')
  2. const bodyParser = require('body-parser')
  3. const app = express()
  4. const port = 3000
  5.  
  6. app.use(bodyParser.json())
  7. app.use((req, res, next) => {
  8. res.setHeader('Access-Control-Allow-Origin', 'http://localhost:3000')
  9. //res.setHeader('Access-Control-Allow-Origin', '*')
  10. res.setHeader('Access-Control-Allow-Methods', 'GET')
  11. res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type')
  12. res.setHeader('Access-Control-Allow-Credentials', true)
  13. next()
  14. })
  15.  
  16. app.get('/data.json', (req, res) => {
  17. res.set('Content-Type', 'application/json')
  18. res.set('Accept-Charset', 'utf-8')
  19. const data = {'data': 'test'}
  20. res.send(JSON.stringify(data))
  21. })
  22. app.listen(port, () => {
  23. console.log(`Servidor rodando http://localhost:${port}/data.json`)
  24. })
  25.  
  26. //resquisição no cliente
  27. fetch('http://localhost:3000/data.json')
  28. .then(response => response.json())
  29. .then(response => console.log(response))
  30. .catch(err => console.error('Failed retrieving information', err))
Add Comment
Please, Sign In to add comment