При использовании ngrok получаю ошибку EventSource's response has a MIME type ("text/html") that is not "text/event-stream". Aborting the connection. С локальным хостом все работает.
Исходники сервера :
const express = require('express')
const cors = require('cors');
const app = express();
const port = 3000;
var corsOptions = {
origin: 'http://example.com',
optionsSuccessStatus: 200
}
app.get('/', cors(corsOptions), (req, res) => {
console.log('Client connected')
res.setHeader('Access-Control-Allow-Credentials', true)
res.set('Access-Control-Allow-Origin', '*')
res.set('Access-Control-Allow-Methods', 'GET, OPTIONS')
res.setHeader('Content-Type', 'text/event-stream')
const intervalId = setInterval(() => {
const date = new Date().toLocaleString()
res.write(`data: ${date}\n\n`)
}, 1000)
res.on('close', () => {
console.log('Client closed connection')
clearInterval(intervalId)
res.end()
})
})
app.listen(port, () => {
console.log(`Server running on port ${port}`)
})
Исходник клиента :
const es = new EventSource('https://e640-77-95-90-50.eu.ngrok.io');
function udateMessage (message) {
const list = document.getElementById('messages');
const item = document.createElement('p');
item.textContent = message
list.appendChild(item)
}
es.onmessage = function(event) {
console.log(event.data);
udateMessage(event.data)
}
const btn = document.querySelector('.btn');
btn.addEventListener('click', function (){
});
Почему он content type приводит к text/html