标签: nodejs-stream

如何在nodejs中从远程url创建可读流?

在nodejs文档中,streams部分说我可以做fs.createReadStream(url || path)。但是,当我真正这样做时,它告诉我Error: ENOENT: no such file or directory。我只想将视频从可读流传输到可写流,但我坚持创建一个可读流。

我的代码

const express = require('express')
const fs = require('fs')
const url = 'https://www.example.com/path/to/mp4Video.mp4'
const port = 3000

app.get('/video', (req, res) => {
    const readable = fs.createReadStream(url)
})
app.listen(port, () => {
    console.log('listening on port ' + port)
})
Run Code Online (Sandbox Code Playgroud)

错误:

listening on port 3000
events.js:291
      throw er; // Unhandled 'error' event
      ^

Error: ENOENT: no such file or directory, open 'https://www.example.com/path/to/mp4Video.mp4'
Emitted 'error' event on ReadStream instance …
Run Code Online (Sandbox Code Playgroud)

video-streaming node.js nodejs-stream nodejs-server

25
推荐指数
1
解决办法
4万
查看次数

Socket.io-Stream不发送给客户端

我正在尝试从服务器向客户端发送(中继)连续的utf-8数据流.虽然我可以关注到达服务器的数据,但我无法将其传输到Socket并将其转发给客户端.

Nodejs服务器,

var io = require('socket.io')(server);
app.io = io;
var dsteem = require('dsteem')
var es = require('event-stream') 
var client = new dsteem.Client('https://api.steemit.com')
var ss = require('socket.io-stream'); 
var outBoundStream = ss.createStream();
var stream = client.blockchain.getBlockStream();

io.on('connection', function(socket) {
    /* Eyeball the data in the nodejs console */ 
    stream.pipe(es.map(function(block, callback) {
    callback(null, util.inspect(block, {colors: true, depth: null}) + '\n')
    })).pipe(process.stdout);
    /* Send stream data to client */
    ss(socket).on('ready', function(){
        console.log('Here it comes...');
        ss(socket).emit('sending', function(){
            stream.pipe(es.map(function(block, callback) {
            callback(null, util.inspect(block, {colors: true, depth: …
Run Code Online (Sandbox Code Playgroud)

javascript socket.io nodejs-stream

12
推荐指数
1
解决办法
589
查看次数

“chunk”参数必须是字符串类型或 Buffer 的实例

我正在运行以下代码,但由于以下错误而失败。

列出bucker内所有对象的AWS代码

const http = require('http');
const host = '127.0.0.1';
const port = 5000;
const path = require('path')
const url = require('url')
const fs = require('fs')
var AWS = require('aws-sdk');



const laptopDate = JSON.parse(fs.readFileSync(`${__dirname}/data/data.json`, `utf-8`))

AWS.config.update({accessKeyId: '***', secretAccessKey: '***', region: 'ap-south-1'});
s3 = new AWS.S3({apiVersion: '2006-03-01'});

var params = { 
    Bucket: 'bucket-name'
}

const server = http.createServer(function(req, res){
    const path = url.parse(req.url, true).pathname
    const id = url.parse(req.url, true).query.id

    if (path === 'bucket' || path === '/')
      s3.listObjects(params, function (err, data) …
Run Code Online (Sandbox Code Playgroud)

javascript node.js aws-sdk-nodejs nodejs-stream nodejs-server

9
推荐指数
2
解决办法
2万
查看次数

单元测试使用模拟使用请求,管道和流的私有方法

我想在下面的代码中对导出的方法进行单元测试.我想模拟私有方法中的值来控制返回的Promise的拒绝/解析.client是已连接到数据库的node-postgres对象.

我知道我可以使用proxyquire来存根所需的库,但是如何模拟链接的方法.on('error', ...),.pipe(stream)以及.on('end', ...)我可以控制返回的值.

请注意,显示的导出方法是对实际方法的简化,导出importDomain是不可行的.

const copyFrom = require('pg-copy-streams').from
const request = require('request')
const Promise = require('bluebird')

// private
function importDomain (client, domain) {
    return new Promise((resolve, reject) => {
    let stream = client.query(copyFrom(`COPY ${domain.table} FROM STDIN;`))

    let req = request(`${domain.url}`)
    req.on('error', reject)
    req.pipe(stream)
       .on('error', reject)
       .on('end', resolve)
  })
}

// public
module.exports = (client) => {
  let domain = someFunctionReturningDomain()
  importDomain(client, domain)
}
Run Code Online (Sandbox Code Playgroud)

node.js proxyquire nodejs-stream pg-copy

7
推荐指数
1
解决办法
715
查看次数

根据mongodb typings弃用find方法

我目前有这个电话:

 const q = coll.find(query, {
    tailable: true,
    awaitData: true,
    oplogReplay: true,
    noCursorTimeout: true,
    numberOfRetries: Number.MAX_VALUE
  });

 return q.stream()
Run Code Online (Sandbox Code Playgroud)

但我的IDE警告我,mongodb.Collection上的这个方法已被弃用:

在此输入图像描述

果然这就是打字所说的.我的问题是 - 这里的长期解决方案是什么,同一个电话的新方法是什么?

mongoose mongodb node.js mongodb-query nodejs-stream

7
推荐指数
1
解决办法
902
查看次数

如何在没有管理员权限的情况下安装节点版本管理器(NVM)

我的 Windows 机器没有管理员权限。我可以在没有管理员权限的情况下安装 NVM 吗?我尝试使用环境变量路径设置,但它在我的情况下不起作用。

node.js node-modules google-api-nodejs-client nodejs-stream alexa-sdk-nodejs

7
推荐指数
2
解决办法
3万
查看次数

Node.js HTTP - TypeError:标头内容包含无效字符

const http = require('http');

const req = http.request({
  method: 'POST',
  hostname: 'cloudsso?test.myco.com',
  port: 80,
  path: '/as/token.oauth2',
  headers: {
    'Content-Type': 'application/json',
  },
  agent: false  // create a new agent just for this one request

}, function (res) {

  res.on('headers', function (h) {
    console.log('headers => ', h);
  });

  let data = '';

  res.on('data', function (d) {
    data += d;
  });

  res.once('end', function () {
    console.log('data => ', data);
  });

});

req.write(JSON.stringify({
  client_id: 'xxx',
  client_secret: 'secret',
  grant_type: 'refresh_token',
}));

req.end();
Run Code Online (Sandbox Code Playgroud)

我运行此代码,我收到以下错误:

_http_outgoing.js:358
    throw …
Run Code Online (Sandbox Code Playgroud)

http http-post node.js nodejs-stream

6
推荐指数
3
解决办法
1万
查看次数

_read()未在可读流上实现

这个问题是如何真正实现可读流的read方法。

我有一个Readable流的实现:

import {Readable} from "stream";
this.readableStream = new Readable();
Run Code Online (Sandbox Code Playgroud)

我收到此错误

events.js:136 throw er; //未处理的“错误”事件^

错误[ERR_STREAM_READ_NOT_IMPLEMENTED]:未在Readable._read(_stream_visible.js:445:10)处在readable.read(_stream_read.js:445:10)在_combinedTickCallback处的履历(_stream_read.js:445:10)上​​未实现_read() (内部/流程/next_tick.js:138:11)在启动时(bootstrap_node)在Function.Module.runMain(模块.js:684:11)的process._tickCallback(内部/流程/next_tick.js:180:9) js:191:16)在bootstrap_node.js:613:3

错误发生的原因很明显,我们需要这样做:

  this.readableStream = new Readable({
      read(size) {
        return true;
      }
    });
Run Code Online (Sandbox Code Playgroud)

我不太了解如何实现read方法。

唯一可行的就是打电话

this.readableStream.push('some string or buffer');
Run Code Online (Sandbox Code Playgroud)

如果我尝试做这样的事情:

   this.readableStream = new Readable({
          read(size) {
            this.push('foo');   // call push here!
            return true;
          }
     });
Run Code Online (Sandbox Code Playgroud)

然后什么也没发生-可读性就没有了!

此外,这些文章说您不需要实现read方法:

https://github.com/substack/stream-handbook#creating-a-visible-stream

https://medium.freecodecamp.org/node-js-streams-everything-you-need-to-know-c9141306be93

我的问题是 -为什么在read方法中调用push不起作用?对我唯一有用的就是在其他地方调用read.push()。

node.js node-streams nodejs-stream

6
推荐指数
2
解决办法
5586
查看次数

Node.js - 将文件流从一台服务器传输到另一台服务器

我正在尝试使用 Node.js 从远程 URL 获取文件,然后将其发送到另一台服务器(使用两个网站各自提供的 API)。我已经成功使用fs.createReadStream("file.png"). 然而,远程文件似乎是一个不同的故事:我不能简单地把“ https://website.com/file.png ”放在那里,我需要一个相当于远程文件的 createReadStream 。

显然我可以使用单独的命令在本地下载文件并使用 createReadStream 上传它然后删除本地文件,但我希望我的代码高效并且不依赖于手动下载临时文件,而且这是一个很好的学习体验。因此,我想知道在两个不同服务器之间将文件作为流传输的最简单方法。

另外,如果可能的话,我想避免使用额外的依赖项,因为我正在编写一个简单的脚本,我不想依赖太多的 npm 包。我主要依靠require("https")require("fs")。我很好奇这是否可以通过简单的调用来实现https.get()

javascript https http node.js nodejs-stream

6
推荐指数
0
解决办法
2912
查看次数

如何在节点 js 中写入 xls 文件和流以响应

我尝试过的内容如下:

exports.downloadListOfUsers = (req, res) => {
    let users = [{id:'',name:'',lastName:'',gender:''},{...},{...}]
    const XLSX = require('xlsx');
    let ws = XLSX.utils.aoa_to_sheet(users); //XLSZ.utils.json_to_sheet(users)
    let wb = XLSX.utils.book_new();
    let wbout = XLSX.write(wb, {bookType: 'xlsx', type: 'binary'});
    wbout.pipe(res)
}
Run Code Online (Sandbox Code Playgroud)

将其发送到客户端后,响应状态为待处理,一段时间后失败。我不知道我错过了什么。请指导我。谢谢。

javascript excel xlsx node.js nodejs-stream

6
推荐指数
1
解决办法
4846
查看次数