小编Zee*_*mon的帖子

Facebook与NodeJS连接

我正在创建一个带有express.js和angular,js的web应用程序,客户端通过facebook进行身份验证.对于facebook身份验证,我使用facebook-node-sdk连接facebook并检索详细信息.

通过url重定向进行身份验证时,代码运行良好.但是当我POST从客户端发送请求以调用相同的代码时,相同的代码不返回任何结果

服务器代码

app.post('/login', Facebook.loginRequired(), function(req, res){
  var result = {};
  req.facebook.api('/me', function(err, user) {
      console.log(user);
      if(user !== undefined){
        result.status = 1;
        result.name = user.name;
        res.contentType('json');
        res.write(JSON.stringify(result));
        res.render('index')
        res.end();
      }
      else{
        result.status = 0;
        result.name = "Error signing into Facebook";
        res.contentType('json');
        res.write(JSON.stringify(result));
        res.render('/')
        res.end();
      }
    });
});
Run Code Online (Sandbox Code Playgroud)

调用上面的代码时没有错误.console.log说明:

POST /登录302.

那么可以通过POST请求验证Facebook,还是应该始终是直接的URL请求?如果是这样的任何选项通过POST进行身份验证?

fbconnect node.js facebook-javascript-sdk facebook-node-sdk

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

mongoose对象的最佳实践

我有一个user.js文件,其中包含我所有的mongoose模式和方法,大致如下:

var userSchema = mongoose.Schema({
  name: { type: String, required: true, trim: true },
  surname: { type: String, required: true },
});

var User = mongoose.model('User', userSchema);
Run Code Online (Sandbox Code Playgroud)

现在,最好公开模拟数据的函数吗?像这样:

exports.addUser = function(data, next){
  user = new User(data);
  user.save(next);
};

exports.getAll = function(next){
  var query = User.find();
  query.exec(next);
};
Run Code Online (Sandbox Code Playgroud)

或者只露出User要在别处使用的对象?

module.exports = User; //or
exports.User = User;
Run Code Online (Sandbox Code Playgroud)

我也面临着从第二个解决方案中得到的问题,假设我想向我添加一个汽车列表userSchema,并且汽车架构在另一个文件car.js中定义,那么这意味着我将不得不carSchema为我的用户公开.js文件,对吗?这意味着我实际上取消了上面提供的第二个解决方案,那么,做这种事情的正确方法是什么?

mongoose mongodb node.js mongoose-schema

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

如何从node.js服务器检测客户端断开连接

我是node.js的新手.如何检测客户端与node.js服务器断开连接.

这是我的代码:

var net = require('net');
var http = require('http');

var host =  '192.168.1.77';
var port = 12345;//
var server = net.createServer(function (stream) {
stream.setEncoding('utf8');

stream.on('data', function (data) { 
    var comm = JSON.parse(data); 
    if (comm.action == "Join_Request"  && comm.gameId =="game1") // join request getting from client
    {
        var reply0 = new Object();
        reply0.message = "WaitRoom";
        stream.write(JSON.stringify(reply0) + "\0");   

    }
});

stream.on('disconnect', function() {
});
stream.on('close', function () {
console.log("Close");
}); 
stream.on('error', function () { 
console.log("Error");
}); 

});  

server.listen(port,host);
Run Code Online (Sandbox Code Playgroud)

如何知道客户端互联网断开连接.

javascript disconnect node.js

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

node.js v10.0.0搞砸了

我有一个node.js api项目,它可以与node.js v8.1.4和npm 很好地协作,v5.0.3但是当我移到node.js v10.0.0和npm时v5.6.0,它的结果是:

core-api@0.0.2生态系统E:\ opensource \ node-cheat \ core-api gulp编译&& pm2启动生态系统.config.js | 班扬

gulp [11100]:src \ node_contextify.cc:631:断言gulp args[1]->IsString()' failed. 1: node::DecodeWrite 2: node::DecodeWrite 3: uv_loop_fork 4: v8::internal::interpreter::BytecodeDecoder::Decode 5: v8::internal::RegExpImpl::Exec 6: v8::internal::RegExpImpl::Exec 7: v8::internal::RegExpImpl::Exec 8: 000000E5F6F84281 npm ERR! code ELIFECYCLE npm ERR! errno 134 npm ERR! core-api@0.0.2 ecosystem: 编译&& pm2启动ecosystem.config.js | bunyan` npm ERR!退出状态134 npm错误!npm ERR!无法通过core-api@0.0.2生态系统脚本执行。npm ERR!npm可能不是问题。上面可能还有其他日志记录输出。

npm ERR!可以在以下位置找到该运行的完整日志:npm ERR!
C:\ Users \ zeeshan \ AppData \ Roaming \ npm-cache_logs \ 2018-05-06T11_29_32_241Z-debug.log

如何解决?

javascript node.js npm gulp pm2

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

使用nodejs上传文件到s3

我已经编写了一个使用 上传文件到 s3 的函数aws-sdk,但我遇到了问题。

当我替换Body时,Body: "test"该函数将成功执行,我可以test看到s3.

但是我想上传一个文档,例如pdf文件,它无法运行并抛出错误:

UnhandledPromiseRejectionWarning:类型错误 [ERR_INVALID_ARG_TYPE]:“path”参数必须是字符串、缓冲区或 URL 类型之一。在新的 ReadStream (internal/fs/streams.js:99:10) 处的 ReadStream.open (internal/fs/streams.js:110:12) 处的 Object.open (fs.js:406:3) 处接收到类型对象Object.createReadStream (fs.js:1725:10)

这是我的代码:

import { createWriteStream, createReadStream } from "fs";
import AWS from 'aws-sdk';
const fs = require('fs');


const uploadS3 = async ({file}) => {
  const { stream, filename} = file;
  AWS.config.update({
    accessKeyId: '*******',
    secretAccessKey: '********',
    region: 'us-east-1' 
  });

  let params = {
    Bucket: "test-files-staging",
    Key: 'documents/' + filename, …
Run Code Online (Sandbox Code Playgroud)

javascript file-upload amazon-s3 node.js aws-sdk

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

Busboy 文件大小限制事件未被调用

我正在使用 Express 将文件上传到 MongoDb 中。一切工作正常,但现在我正在尝试设置大小限制。将busboy配置设置为 Busboy站点声明:

 var busboy = new Busboy({ headers: req.headers,  limit: { files: 1, fileSize: 50000 }  });
Run Code Online (Sandbox Code Playgroud)

然后:

file.on('limit', function(data) {                                               
     console.log("LIMIT");
});
Run Code Online (Sandbox Code Playgroud)

顺便说一句,当数据卡盘加起来达到限制大小时,我可以尝试通过停止一切来找到解决方法。

file.on('data', function(data) {                                          
   fileSize+=data.length;
    if (fileSize>50000){
        //limit reached 
   }                         
});
Run Code Online (Sandbox Code Playgroud)

limit mongodb node.js express busboy

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

如何将 UTC 日期时间转换为指定的时区?

看起来很简单,但我没有找到将 UTC 日期时间转换为指定时区的方法。我找到了如何将 UTC 日期时间转换为本地日期时间,但现在我想转换为特定时区(例如莫斯科时间)

例如在 c# 中,我们可以这样做:

// user-specified time zone
TimeZoneInfo southPole =
    TimeZoneInfo.FindSystemTimeZoneById("Antarctica/South Pole Standard Time");

// an UTC DateTime
DateTime utcTime = new DateTime(2007, 07, 12, 06, 32, 00, DateTimeKind.Utc);

// DateTime with offset
DateTimeOffset dateAndOffset =
    new DateTimeOffset(utcTime, southPole.GetUtcOffset(utcTime));

Console.WriteLine(dateAndOffset);
Run Code Online (Sandbox Code Playgroud)

但是在Delphi中怎么办呢?

windows delphi winapi timezone datetime

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

如何在angular js中使用$ http GET方法发送数据?

我需要将emailId作为数据从angularjs控制器发送到nodejs。我用谷歌搜索,但没有得到解决方案,请有人帮我。

控制器文件:

function ManageProductController($http, $scope, $mdDialog, $document, $location, $localStorage)
{
     var vm = this;
     vm.email = $localStorage.email;

     $http({
            url: 'http://localhost:7200/api/manage-product',
            method: 'GET',
            data: {email:vm.email}
        }).success(function(res) {
            //$scope.productlist = res;
            //console.log(res.result);
            vm.result=res.result;

            //vm.docs=res.docs;
        }, function(error) {
            console.log(error);
            alert('here');
        });
}
Run Code Online (Sandbox Code Playgroud)

在上面的代码中,我已经email作为数据发送了,但是在node.js文件中,我没有收到请求。

节点文件:

 router.get('/manage-product', function(req, res){
    //console.log('I received get request');

    console.log(req);
    var findProducts = function(db, callback) {
       var cursor =db.collection('proInfo').find().toArray(function(err, docs){
          if(err){
             callback(new Error("Some problem"));
           }else{
            callback(null,docs);
        } 
         });

    };
}
Run Code Online (Sandbox Code Playgroud)

console.log(req);在这里输入了内容,但是在正文部分中,我只是body{}这样输入。

node.js express angularjs

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