小编Mar*_*hii的帖子

Node.js将相同的可读流管道化为多个(可写)目标

我需要串行运行两个需要从同一个流中读取数据的命令.将流传输到另一个流后,缓冲区被清空,因此我无法再次从该流中读取数据,因此这不起作用:

var spawn = require('child_process').spawn;
var fs = require('fs');
var request = require('request');

var inputStream = request('http://placehold.it/640x360');
var identify = spawn('identify',['-']);

inputStream.pipe(identify.stdin);

var chunks = [];
identify.stdout.on('data',function(chunk) {
  chunks.push(chunk);
});

identify.stdout.on('end',function() {
  var size = getSize(Buffer.concat(chunks)); //width
  var convert = spawn('convert',['-','-scale',size * 0.5,'png:-']);
  inputStream.pipe(convert.stdin);
  convert.stdout.pipe(fs.createWriteStream('half.png'));
});

function getSize(buffer){
  return parseInt(buffer.toString().split(' ')[2].split('x')[0]);
}
Run Code Online (Sandbox Code Playgroud)

请求抱怨这个

Error: You cannot pipe after data has been emitted from the response.
Run Code Online (Sandbox Code Playgroud)

并改变inputStreamfs.createWriteStream当然会产生同样的问题.我不想写入文件,而是以某种方式重用请求生成的流(或其他任何内容).

有没有办法在完成管道后重复使用可读流?完成上述例子的最佳方法是什么?

javascript pipe stream node.js node.js-stream

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

在Node.js中通过HTTP发送大图像数据

在我的开发环境中,我有两台服务器.一个通过POSThttp请求发送和映像到另一个.

客户端服务器这样做:

    fs.readFile(rawFile.path,'binary',function (err, file){
        restler.post("http://0.0.0.0:5000",{
            data: file,
            headers:{
                "Content-Type": rawFile.type,
            }
        }).on('complete',function(data,response){                               
            console.log(data);
            res.send("file went through")
        })
Run Code Online (Sandbox Code Playgroud)

收到请求的服务器执行此操作:

    server.post('/',function(req,res,next){
        fs.writeFileSync("test.png",req.body,"binary",function(err){
            if(err) throw err;
            res.send("OK")
        })
    })
Run Code Online (Sandbox Code Playgroud)

如果我发送一个小图像,它工作正常.但是,如果我发送大图像虽然文件保存正确,但只显示图像的第一个上半部分.其余的都是黑色的.图像大小是正确的.

我想这只是文件中写入的第一块图像.我试过创建一个readStream和一个writeStream但它似乎不起作用:

req.body.pipe(fs.createWriteStream('test.png'))
Run Code Online (Sandbox Code Playgroud)

我可以直接从二进制数据流传输pipe到文件中吗?对于我所看到的,readStream通常用于从文件流而不是原始二进制数据.

我读了几篇文章,但它似乎对我不起作用.

restler在客户端服务器和restify另一个中使用模块.

谢谢!

image http node.js

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

动态更改Backbone中的URL

我试图动态更改路由器内的url但无法设法执行此操作,它会一直返回到基本的Collection URL.在这里,我发布了代码与3个不同的集合,除了指向三个不同的网址,他们完全相同.我只有一个model和三个collections依赖于该模型,他们甚至渲染相同的视图.我怎样才能动态改变url所以我只能创建一个Collection和一个Model?对于像这样的案件,这是最好的实践吗?

    // MODELS & COLLECTIONS 
    window.Post = Backbone.Model.extend({
            urlRoot:  function() {
                    return 'http://localhost:5000/json/guides/:id'
            }
    })

    App.Collections.RecentPosts = Backbone.Collection.extend({
            model: Post,
            url:'http://localhost:5000/json/posts/recent',
    })
    App.Collections.PopularPosts = Backbone.Collection.extend({
            model: Post,
            url:'http://localhost:5000/json/posts/popular',
    })
    App.Collections.FeaturedPosts = Backbone.Collection.extend({
            model: Post,
            url:'http://localhost:5000/json/posts/featured',
    })

    // CONTROLLER
    App.Controllers.Documents = Backbone.Router.extend({
            routes:{
            "recent"                : "recent",
            "popular"         : "popular",
            "featured"          : "featured",
            },

            recent: function(){
                //.... same as featured ?
            },
            popular: function(){
                //.... same as featured ?
            },
            featured: …
Run Code Online (Sandbox Code Playgroud)

javascript json backbone.js

5
推荐指数
1
解决办法
3594
查看次数

CSS视角不起作用

我正在尝试用CSS创建一个多维数据集.我实际上认为它已经存在但我看不到它.随意编辑小提琴.

我不明白为什么这个观点不起作用.这是最佳做法吗?

是否可以整体旋转立方体?

来源:24ways.

HTML:

<section class="container">
      <div id="cube">
        <figure class="front">1</figure>
        <figure class="back">2</figure>
        <figure class="right">3</figure>
        <figure class="left">4</figure>
        <figure class="top">5</figure>
        <figure class="bottom">6</figure>
      </div>
    </section>
Run Code Online (Sandbox Code Playgroud)

CSS:

.container {
  margin: 200px auto;
  width: 200px;
  height: 200px;
  position: relative;
  -webkit-perspective: 800px;
 }

 #cube {
  width: 100%;
  height: 100%;
  position: absolute;
 -webkit-transform-style: preserve-3d;
 }

#cube figure {
  width: 198px;
  height: 198px;
  display: block;
  position: absolute;
  border: 1px solid #ddd;
  background-color: rgba(0,0,0,0.2);
}

#cube .front { -webkit-transform: rotateY(0deg) translateZ(100px); }
#cube .back …
Run Code Online (Sandbox Code Playgroud)

html5 transform css3

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

在Backbone中有条件地绑定事件 - 移动或桌面

为了更容易阅读,我把所有东西都放在了这个initialize功能之下.这里有什么问题吗?警报被触发,因此不是条件.我隐藏了共享操作,并希望在桌面上显示它们并在悬停不可能的情况下点击移动设备.我在这里错过了什么吗? console.log()不会抛出任何错误.

App.Views.Title = Backbone.View.extend({

initialize:function(){

    _.bindAll(this,"stickToTop");
    this.template = _.template($("#title").html());
    this.render();
    $(window).scroll(this.stickToTop);

    var isMobile = navigator.userAgent.match(/(iPhone|iPod|iPad|Android|BlackBerry)/);
    var share = this.$(".share");

    if(isMobile){

        // alert('mobile')
        share.on('click' , this.shareMobile , this);

    }else{
        // alert('not mobile')
        share.on('hover' , this.shareDesktop , this);

    }

},
...
Run Code Online (Sandbox Code Playgroud)

javascript jquery backbone.js backbone-events

2
推荐指数
1
解决办法
2742
查看次数

Backbone.js App方法不是很可扩展 - 嵌套视图

我正在创建一个Backbone App,虽然一切似乎都有效但我担心如果我继续使用这种方法添加功能和视图,我可能需要很快重构.我在后端使用Parse,这意味着该对象Parse等同于Backbone.Model.我无法控制我的模型,因为它们已经针对移动设备进行了优化,在这里我只需要显示3种不同型号的某些键值.

虽然没有什么"错误"(它有效)但我开始做"更大"的错误只是时间问题.

这是我的Router:

App.Controllers.Documents = Backbone.Router.extend({
    routes:{
        "story/:id" : "fullStory",
        "" : "index"
    },
// ...
   fullStory: function(id){
     var self = this;
     var query = new Parse.Query(Steps);
     query.equalTo("story", id)
     var steps = query.collection();

     steps.fetch({
       success: function() {

         new App.Views.Steps({ collection: steps });

         var title = new Story({ objectId: id });
         title.fetch({
              success: function(model, resp) {
              new App.Views.Title({ model: title});
              var idUser = title.toJSON().idUser;
              var user = new Parse.User({ objectId: idUser }); …
Run Code Online (Sandbox Code Playgroud)

javascript jquery backbone.js

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

Node.js Passport反序列化用户返回20个以上的缩进用户

一切似乎都有效.对Parse.com的请求检查用户是否存在并创建或检索它.我有两个主要问题.首先,当deserializeUser调用该函数时,它返回20个相同的用户,并且该findUser函数应该返回这20个用户.另外,我无法访问req.user

    passport.serializeUser(function(user, done) {

            done(null, user.objectId);
    });

    passport.deserializeUser(function(uid, done) {

            console.log(uid)
            // This outputs the ObjectId 20 TIMES!! ??

            Parse.findUser(uid,function(error,user){

                done(null, user);
            })
    });

    // CONFIGURATION
    app.configure(function() {
            app.use(express.bodyParser()); //read
            app.use(express.cookieParser()); //read
        app.use(express.session({ secret: process.env.SESSION_SECRET || 'abcde' }));
        app.use(passport.initialize());
        app.use(passport.session());
            app.use(app.router);
            app.use(express.static(__dirname + '/static'))
    });

    passport.use(new FacebookStrategy({
            clientID: process.env.FACEBOOK_APP_ID || 'app_id',
            clientSecret: process.env.FACEBOOK_SECRET || 'fb_secret',
        callbackURL: "http://localhost:5000/auth/facebook/callback"
        },
        function(accessToken, refreshToken, profile, done) {

            process.nextTick(function() {
                    _parse.user(accessToken, profile,function(parseUser){
                        //this returns the user.....

                    return done(null, …
Run Code Online (Sandbox Code Playgroud)

facebook node.js parse-platform passport.js

0
推荐指数
1
解决办法
1569
查看次数