小编Ale*_*lls的帖子

JavaScript - 为什么我们必须从自调用函数返回一个函数?

此代码有一个运行时错误:

var person = (function(){

    var Person = {

        init: function() {

        },
    };

    return new Person();

    /*return function(){
        new Person();
    }*/

})();

console.log(person);
Run Code Online (Sandbox Code Playgroud)

它说我必须返回一个函数而不是一个普通的Object.

为什么我不能从自调用/匿名外部函数返回一个对象?为什么我必须返回一个函数?

同样,这个改变的代码也给我一个类似的错误:

var person = function(){

    var Person = {

        init: function() {

        },

    };

    return new Person();

    /*return function(){
     new Person();
     }*/

};

console.log(person());
Run Code Online (Sandbox Code Playgroud)

javascript node.js

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

Node.js Express - next()回调

在Node.js Express中 - 使用app.use函数 -

为什么我不必这样做:

app.use(function(req,res,next){
    //do something here
    next(req,res);
});
Run Code Online (Sandbox Code Playgroud)

通常我只是这样做而且有效

app.use(function(req,res,next){
    //do something here
    next();
});
Run Code Online (Sandbox Code Playgroud)

node.js express

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

Express - 防止发送两个以上的响应

使用 Node.js Express 服务器,如果我们尝试向同一请求发送两个响应,我们会收到错误“无法发送标头两次......”。我想找出防止发送两个响应的最佳方法,对于这种特殊情况,但也可能适用于其他情况。

我有一个简单的 /test 路由,它返回有关服务器状态的信息,如下所示:

app.get('/test', function (req, res, next) {

    var client = redis.createClient();

    var response = {};

    var alreadySentResponse = false; //I use a boolean to check if I already sent a response

    client.on("error", function (err) {   //this callback will invoke res.send
        log.error('error in redis client test error callback:', err);
        if(alreadySentResponse === false){
            alreadySentResponse = true;
            res.status(500).send({"test message from SmartConnect": err});
        }

    });

    client.info(function (err, resp) { //this callback will also invoke res.send
        response['redisInfo'] = resp; …
Run Code Online (Sandbox Code Playgroud)

node.js express

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

将一个文件中的字符串替换为另一个文件的内容

我正在寻找一个BASH程序,我可以运行以使用file1中的所有内容替换file2中的匹配字符串.

所以file2看起来像这样:

define([
 'require'
  ****
 ],
 function(require){
 });
Run Code Online (Sandbox Code Playgroud)

file1看起来像:

, 'app/js/controllers/x'
, 'app/js/controllers/y'
, 'app/js/controllers/z'
Run Code Online (Sandbox Code Playgroud)

有一个简单的BASH脚本,我可以用来从file1复制3行,并用****file1内容替换字符串,然后将结果写入file3.js?

bash

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

没有Express的简单Node.js服务器 - 管理HTML文件以响应

我正在努力制作一个没有Express的简单Node.js服务器,这实际上我实际上学习了更多关于基于路径请求和基本HTTP内容的实际服务器文件和请求数据的故障.

我有这样简单的服务器,如下所示:

 var http = require('http');

const PORT = 6969;

var allRoutes = require('./routes/all');

var server = http.createServer(allRoutes);

server.listen(PORT, function () {
    console.log("Server listening on: http://localhost:%s", PORT);
});
Run Code Online (Sandbox Code Playgroud)

然后我有一个像这样处理所有请求的"中间件"函数:

var url = require('url');
var fs = require('fs');
var appRootPath = require('app-root-path');
var path = require('path');

function handleRequest(req, res) {

    var requestUrl = url.parse(req.url);


    var fsPath;
    if (requestUrl.pathname === '/') {
        fsPath = path.resolve(appRootPath + '/view/index.html');
    }
    else {
        fsPath = path.resolve(appRootPath + '/view/' + requestUrl.pathname);
    }

    fs.stat(fsPath, function (err, stat) …
Run Code Online (Sandbox Code Playgroud)

javascript node.js

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

socket.io 监听/监听事件

我很惊讶地看着socket.io文档,当 socket.io 绑定到一个端口时,它没有触发任何事件......我正在寻找一个“监听”/“监听”事件......

http://socket.io/docs/server-api/

我有一个用 http.Server 实例初始化的简单模块:

var io = require('socket.io');

var socketServer = null;

function getSocketServer(httpServer) {

    if (socketServer === null) {

        if (httpServer == null) {
            throw new Error('need to init socketServer with http.Server instance');
        }

        socketServer = io.listen(httpServer);
    }

    return socketServer;

}


module.exports = {
    getSocketServer:getSocketServer
};
Run Code Online (Sandbox Code Playgroud)

当我需要这个模块时,我想监听一个“监听”事件。

就像是:

var socket = require('./socket-cnx').getSocketServer();

socket.on('listening',function(err){

});
Run Code Online (Sandbox Code Playgroud)

我想主要原因是因为onAPI 用于事件名称。

node.js socket.io

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

在服务器之间发送 cookie 与发送标头

我对如何在服务器之间发送 cookie 数据有点天真。我知道您在 HTTP 请求中使用了 Set-Cookie。

现在,出于授权目的,我在服务器之间发送标头,以便一台服务器获得另一台服务器的授权。但我想知道使用 cookie 是否有一些优势,如果在这种情况下 cookie 的行为与标头不同。据我所知,cookie 和标头对于大多数用途来说是一回事吗?

使用两台 Node.js 服务器,一台是 Web 服务器,另一台是 API 服务器,是否有任何理由可以解释为什么发送 cookie 比发送常规非 cookie 标头更好?

cookies http http-headers node.js express

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

Node.js事件发射器源代码的说明

我正在寻找Node.js事件发射器的源代码

https://github.com/nodejs/node/blob/master/lib/events.js

我试图弄清楚代码如何识别功能,特别是在使用addListener/时removeListener

这些函数都接受(String s,Function f)的签名

但我不了解的是它们如何识别调用removeListener时要删除的函数,因为可能有多个函数充当同一事件的回调。

我想我特别想知道这条线

list[i] === listener
Run Code Online (Sandbox Code Playgroud)

也就是说,比较两个函数是否相等,可以在JS中使用

javascript node.js

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

Redis NOAUTH 错误 - 尽管在我的 redis.conf 文件中禁用了身份验证

针对本地 Redis 实例在本地运行应用程序时,我看到以下错误。

ReplyError: Ready check failed: NOAUTH Authentication required.
            at JavascriptReplyParser.Parser.returnError (/Users/Olegzandr/WebstormProjects/node_redis/index.js:193:31)
            at JavascriptReplyParser.run (/Users/Olegzandr/WebstormProjects/node_redis/node_modules/redis-parser/lib/javascript.js:135:18)
            at JavascriptReplyParser.execute (/Users/Olegzandr/WebstormProjects/node_redis/node_modules/redis-parser/lib/javascript.js:112:10)
            at Socket.<anonymous> (/Users/Olegzandr/WebstormProjects/node_redis/index.js:269:27)
            at emitOne (events.js:77:13)
            at Socket.emit (events.js:169:7)
            at readableAddChunk (_stream_readable.js:146:16)
Run Code Online (Sandbox Code Playgroud)

我相信我已经通过在配置文件中使用这些行禁用了身份验证:

# By default protected mode is enabled. You should disable it only if
# you are sure you want clients from other hosts to connect to Redis
# even if no authentication is configured, nor a specific set of interfaces
# are explicitly listed using the "bind" directive. …
Run Code Online (Sandbox Code Playgroud)

redis node.js

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

git clone x as"y" - (在本地重命名repo)

这是git clone的文档

https://git-scm.com/docs/git-clone

通常我们会像这样克隆一个回购

git clone x
Run Code Online (Sandbox Code Playgroud)

但是,我想在本地重命名存储库,所以它将类似于

git clone x as y
Run Code Online (Sandbox Code Playgroud)

看看文档,目前尚不清楚这是可能的还是犹太教的,是否有人知道如何做到这一点?

它看起来像这样:

git clone x y
Run Code Online (Sandbox Code Playgroud)

根据文件:

<repository>

    The (possibly remote) repository to clone from. See the URLS section below for more information on specifying repositories.
<directory>

    The name of a new directory to clone into. The "humanish" part of the source repository is used if no directory is explicitly given (repo for /path/to/repo.git and foo for host.xz:foo/.git). Cloning into an existing directory is only allowed if …
Run Code Online (Sandbox Code Playgroud)

git git-clone

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

标签 统计

node.js ×8

express ×3

javascript ×3

bash ×1

cookies ×1

git ×1

git-clone ×1

http ×1

http-headers ×1

redis ×1

socket.io ×1