简体   繁体   English

服务器发送的事件不是“文本/事件流”

[英]Server Sent Events are not “text/event-stream”

I want to add Server Sent Events (SSE) into my project that runs on nodeJS (Express).我想将服务器发送事件 (SSE) 添加到我在 nodeJS (Express) 上运行的项目中。 As a start I have used this code: https://github.com/hnasr/javascript_playground/blob/master/server-sent-events/index.js .作为开始,我使用了以下代码: https://github.com/hnasr/javascript_playground/blob/master/server-sent-events/index.js This example code works like a charm when running it in isolation but when I'm incorporating the code into my (bigger) project I got some errors that I'm unable to solve.这个示例代码在单独运行时就像一个魅力,但是当我将代码合并到我的(更大的)项目中时,我遇到了一些我无法解决的错误。 I hope you can help me.我希望你能帮助我。 I think messed up the code in the routes folder, not sure tho.我认为弄乱了路由文件夹中的代码,不确定。

I use NodeJs with express as my server.我使用带有 express 的 NodeJs 作为我的服务器。 The folder app.js contains this code.文件夹app.js包含此代码。

    #Routing code
    var router_price_24h_changes  = require('./routes/24hours');
    app.use('/24hours', router_price_24h_changes);
    
    #>>>a lot of other (probably unnecessary code)<<<

    #Added this 
    app.get("/24hours-events", (req,res) => {
    
      res.setHeader("Content-Type", "text/event-stream");
      send(res);
      console.log("Stream")
    
    })
    
    let i = 0;
    function send (res) {
      console.log("send")
      res.write("data: " + `hello!${i++}\n\n`);
    
      setTimeout(() => send(res), 1000);
    }

This is what my routes-24hours.js looks like:这就是我的routes-24hours.js样子:

var express = require('express');
var router = express.Router();
const fs = require('fs');


router.get('/', function(req, res, next) {
  
  const myHtml = fs.readFileSync('./24hours.html', 'utf-8');
  const changes = fs.readFileSync("./changes.json", 'utf-8');
  const changes2 = fs.readFileSync("./changes2", 'utf-8');

  res.send(myHtml.replace(/data/g, JSON.stringify(({changes ,changes2 }), null, 0)));

});

module.exports = router;

When visiting the 24hours-events webpage from my chrome browser I typed this从我的 chrome 浏览器访问24hours-events网页时,我输入了这个

let sse = new EventSource("http://localhost:3000/24hours-events");

In the console log, this was the response:在控制台日志中,这是响应:

http://localhost:3000/24hours-events 404 (Not Found)

Note: I have not created a "24hours-events" html file注意:我还没有创建“24 小时事件”html 文件

In the isolated SSE test version I added sse.onmessage = console.log after it and it logged the server events.在隔离的 SSE 测试版本中,我在其后添加了sse.onmessage = console.log并记录了服务器事件。

If something is not clear and more information is needed to answer this question, I would be more then happy to hear from you.如果有不清楚的地方并且需要更多信息来回答这个问题,我会很高兴收到您的来信。

 app.get("http://localhost:3000/24hours", (req,res) => {

When defining a route, you should only specify the path, not the full URL:定义路由时,您应该只指定路径,而不是完整的 URL:

app.get("/24hours", (req, res) => {

The HTML being received might be a 404 page.收到的 HTML 可能是 404 页面。 (You should look in your network panel, or open http://localhost:3000/24hours directly, to confirm that.) (您应该查看您的网络面板,或直接打开 http://localhost:3000/24hours 来确认。)

In the metadata of the 24hours.html file I have added this:在 24hours.html 文件的元数据中,我添加了以下内容:

 <meta http-equiv="Content-Type" content="text/event-stream" charset="utf-8" />

Remove that, it's incorrect.删除它,这是不正确的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Express NodeJS 路由错误文本/html 不是文本/事件流 - Express NodeJS Routing Error text/html is not text/event-stream 是否可以在 WebBrowser 中使用 JavaScript 使用文本/事件流? - Is it possible to consume text/event-stream with JavaScript in a WebBrowser? 当服务器永不停止加载时,如何在 JavaScript 中存储来自 get 请求的初始文本/事件流响应? - How can I store the initial text/event-stream response from a get request in JavaScript when the server never stops loading? 使用事件流结束处理流 - End processing a stream using event-stream EventSource 的响应具有不是“text/event-stream”的 MIME 类型(“text/html”)。 中止连接。 标头设置为文本/事件流 - EventSource's response has a MIME type ("text/html") that is not "text/event-stream". Aborting the connection. header is set to text/event-stream 有没有办法在不使用事件侦听器的情况下从文本/事件流中获得单个响应? - Is there a way to get a single response from a text/event-stream without using event listeners? EventSource的响应具有不是“ text / event-stream”的MIME类型(“ text / html”) - EventSource's response has a MIME type (“text/html”) that is not “text/event-stream” 异步/等待事件流 mapSync 不起作用 - Async/await with event-stream mapSync not working gulp-inject无法与事件流一起使用 - gulp-inject not working with event-stream EventSource的响应具有MIME类型(“text / plain”),而不是“text / event-stream” - EventSource's response has a MIME type (“text/plain”) that is not “text/event-stream”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM