小编msc*_*dex的帖子

清除单个用户的所有会话

在我的网站上,我想要一个组织管理员能够重置用户密码的表单。理想情况下,当重置时,我想从保存 Express 和 Passport 会话数据的 RedisStore 中清除该用户的所有已识别会话。有没有人想出办法做到这一点?

redis node.js passport.js

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

busboy - 有没有办法在所有文件上传后发送响应?

我正在尝试使用node.js作为后端并将angular.js作为前端上传文件到服务器.我正在使用快递4 + busboy.我在前端有一张桌子,我应该显示我正在上传的所有文件.因此,如果我有3个文件并单击上传,angular应该将这些文件发布到node.js,并在获得响应后,使用这三个文件刷新表.这是我在角度中使用的功能:

function uploadFiles(files){
    var fd = new FormData();
    for(var i = 0; i<files.length; i++){
        fd.append("file", files[i]);
    }
    $http.post('http://localhost:3000/upload', fd, {
        withCredentials: false,
        headers: {'Content-Type': undefined },
        transformRequest: angular.identity
    }).success(refreshTable()).error(function(){
        console.log("error uploading");
    });
}
Run Code Online (Sandbox Code Playgroud)

这是来自node.js:

app.post('/upload', function(req, res) {
  var busboy = new Busboy({ headers: req.headers });
  busboy.on('file', function (fieldname, file, filename) {
    console.log("Uploading: " + filename);
    var fstream = fs.createWriteStream('./files/' + filename);
    file.pipe(fstream);
  });
  busboy.on('finish', function(){
    res.writeHead(200, { 'Connection': 'close' });
    res.end("");
  });
  return req.pipe(busboy);
});
Run Code Online (Sandbox Code Playgroud)

问题是,如果我上传了三个文件,一旦上传了第一个文件,node.js就会发送响应,因此只有上传的第一个文件才会更新表,如果我刷新页面,其余的文件会出现.我认为问题在于节点中的这一行:return req.pipe(busboy); …

node.js busboy

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

使用Node.js附加组件实例化和返回C ++包装的对象

我正在使用Node 0.12并尝试创建插件。

我有两个包装好的c ++类,A和B。在类A的一个方法中,我想返回包装的B实例。因此,例如,类A具有如下所示的缩写接口。在A类的方法Api中,我想实例化并返回B类包装的对象。我尝试使用NewInstance(),但不确定如何在NewInstance()为类B定义的方法中调用构造函数。我们将不胜感激您提供的任何帮助。

先感谢您。

-乔恩

class A : public node::ObjectWrap {
public:
static void Init(v8::Handle<v8::Object> exports);

private:
explicit A(Connection *connection);
~B();

static void New(const v8::FunctionCallbackInfo<v8::Value>& args);
static void Api(const v8::FunctionCallbackInfo<v8::Value>& args);

static v8::Persistent<v8::Function> constructor;

Connection* _connection;
};

#endif

class B : public node::ObjectWrap {
public:
static void Init(v8::Handle<v8::Object> exports);


private:

~B();

static v8::Persistent<v8::Function> constructor;
// The method that will construct & return the new JavaScript object

Event* _event;
};

#endif
Run Code Online (Sandbox Code Playgroud)

这是我尝试做的事情NewInstance()

void B::NewInstance(const …
Run Code Online (Sandbox Code Playgroud)

node.js

5
推荐指数
0
解决办法
316
查看次数

node.js事件循环如何记住它需要在长时间运行的操作完成后回调?

我是NODE.JS的新手.我在node.js中读到,只存在单个事件循环(因为它的单线程).

现在假设,案例就是这样,最终用户发出请求,下面的代码片段是用户试图运行的模块的一部分:

....some code before......    
longRunningOperation(argumentsForLongOperation, callbackMethod(argumentsForCallBack));
....some code after......
Run Code Online (Sandbox Code Playgroud)

由于nodejs是异步的,因此事件循环将调用longRunningOperation移动到下一行代码,如果模块中有任何代码行则执行.

现在当longRunningOperation返回时,我的事件循环如何知道它现在应该在longRunningOperation返回时运行回调.

在java中我们有方法堆栈,并且线程停留在longRunningOperation被调用的点,因此当函数返回时,线程从完成调用堆栈后恢复.

我的问题是如何在node.js中知道事件循环longRunningOperation已经返回并且它需要现在调用callBack()函数?

javascript multithreading node.js

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

TypeError:Key必须是新Hmac的缓冲区(crypto.js:91:16)

Server.js

const express = require('express');
const bodyParser = require('body-parser');
const app = express();

// server port is 3000
let port = 3000;

// use bodyParser.json and urlencoded
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
  extended: true,
}));

// use api/v1, require routes/app.js
app.use('/api/v1', require('./routes/app.js')(express));

exports.server = app.listen(port, () => {
  console.log('Server active on port', port);
});
Run Code Online (Sandbox Code Playgroud)

app.js

// require url shortener
const shurl = require('../modules/shurl');

module.exports = (express) => {
  const router = express.Router();

  router.get('/status', (req, res) => {
    res.json({
      healthy: true,
    })
  });

  router.post('/urls', …
Run Code Online (Sandbox Code Playgroud)

node.js

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

在快递上发送标题后无法设置标题

我在这里表达了一个常见的问题.但我无法从各地获得解决方案.这是我的代码:如果row不为空,则渲染代码页,否则执行另一个操作.

app.get('/send',function(req,res){
  var code=req.query['c']; // -- get request from input
  connection.query("use mynum");
  var strQuery = "select * from table WHERE code='"+code+"' LIMIT 1";   
  connection.query( strQuery, function(err, rows){
    if(err) {
      throw err;
    }else{
      if(rows.length==1){
        res.render('pages/code', {code : rows[0].code});
        connection.end();
        res.end();
      }else {
        // here is some actions
      }
    }
  });
  res.end();
});
Run Code Online (Sandbox Code Playgroud)

堆栈跟踪:

Error: Can't set headers after they are sent.
at ServerResponse.OutgoingMessage.setHeader (http.js:690:11)
at ServerResponse.header (C:\wamp\www\vin_number\node_modules\express\lib\re
sponse.js:666:10)
at ServerResponse.res.contentType.res.type (C:\wamp\www\vin_number\node_modu
les\express\lib\response.js:532:15)
at ServerResponse.send (C:\wamp\www\vin_number\node_modules\express\lib\resp
onse.js:121:14)
at fn (C:\wamp\www\vin_number\node_modules\express\lib\response.js:900:10)
at View.exports.renderFile …
Run Code Online (Sandbox Code Playgroud)

javascript mean node.js express

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

访问Angular.js中的会话值

我无法访问Angular.js控制器中node.js设置的会话值.我正在使用Express框架.怎么解决?这是我的代码.

app.use(express.cookieParser());
app.use(express.session({
    secret: '1234567890QWERTY',
    cookie: { httpOnly: false }
     }));
//setting the values
cookies.set('username',username);
req.session.username=username;
Run Code Online (Sandbox Code Playgroud)

node.js angularjs

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

具有Express的Nodejs - 将mongoDB和mongoose添加到package.json并运行npm install时出错


使用Git Bash的Windows 8.1 Express起点使用
python 2.7.10进行npm安装以避免3.4问题

C编译器是否存在问题?
我如何解决它?

的package.json:

{
  "name": "nodeauth",
  "version": "0.0.0",
  "private": true,
  "scripts": {
    "start": "node ./bin/www"
  },
  "dependencies": {
    "body-parser": "~1.13.2",
    "cookie-parser": "~1.3.5",
    "debug": "~2.2.0",
    "express": "~4.13.1",
    "jade": "~1.11.0",
    "morgan": "~1.6.1",
    "serve-favicon": "~2.3.0",
    "mongodb": "*",
    "mongoose": "*"
  }
}
Run Code Online (Sandbox Code Playgroud)

以下日志包含mongodb和mongoose ......当我只包含mongodb时,我有类似的警告:

$ npm install --python=c:\python27\python
-


> kerberos@0.0.12 install e:\nodejs\_projects\nodeauth\node_modules\mongodb\node_modules\mongodb-core\node_modules\kerberos
> (node-gyp rebuild 2> builderror.log) || (exit 0)


e:\nodejs\_projects\nodeauth\node_modules\mongodb\node_modules\mongodb-core\node_modules\kerberos>if not defined npm_config_node_gyp
 (node "c:\Program Files\nodejs\node_modules\npm\bin\node-gyp-bin\\..\..\node_modules\node-gyp\bin\node-gyp.js" rebuild )  else (nod
e\  rebuild )
Building the projects in …

json mongoose mongodb node.js express

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

Node.js 的spawn、exec、选项和编码

根据我的 API 文档(Node.js 最新文档集),Node.js\xe2\x80\x99schild_process.exec options参数有一个encoding成员,而don\xe2\x80\x99toptions参数有一个成员。child_process.spawn

\n\n

为什么会有这样的差异?

\n\n

注意:我是 Node.js 新手

\n

node.js

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

Docker 日志不显示颜色(express + nodejs 图像)

一个小而有趣的问题(对我来说):

我正在尝试从我编写的小型服务器(nodejs + express)创建 docker 映像。我的服务器代码是:

var express = require('express');
var Inflector = require('inflected');
var colors = require('colors');

var app = express();

app.get('/hello/:name', function(req, res, next){
    var name = Inflector.titleize(req.params.name);
    console.log("Saying hello to " + name.yellow);
    res.send('Hello ' + name);
});

var port = 9090;
app.listen(port, function(){
    console.log(('App is running on port ' + port).inverse);
});
Run Code Online (Sandbox Code Playgroud)

我正在用这个 Dockerfile 创建我的图像:

FROM centos:centos6

RUN     rpm -Uvh http://download.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm

RUN     yum install -y npm

COPY . /src

RUN cd /src; npm install

EXPOSE 9090 …
Run Code Online (Sandbox Code Playgroud)

node.js docker

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