Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const express = require('express')
- const bodyParser = require('body-parser')
- const app = express()
- const port = 3000
- app.use(bodyParser.json())
- app.use((req, res, next) => {
- res.setHeader('Access-Control-Allow-Origin', 'http://localhost:3000')
- //res.setHeader('Access-Control-Allow-Origin', '*')
- res.setHeader('Access-Control-Allow-Methods', 'GET')
- res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type')
- res.setHeader('Access-Control-Allow-Credentials', true)
- next()
- })
- app.get('/data.json', (req, res) => {
- res.set('Content-Type', 'application/json')
- res.set('Accept-Charset', 'utf-8')
- const data = {'data': 'test'}
- res.send(JSON.stringify(data))
- })
- app.listen(port, () => {
- console.log(`Servidor rodando http://localhost:${port}/data.json`)
- })
- //resquisição no cliente
- fetch('http://localhost:3000/data.json')
- .then(response => response.json())
- .then(response => console.log(response))
- .catch(err => console.error('Failed retrieving information', err))
Add Comment
Please, Sign In to add comment