小编Dan*_*nce的帖子

该场景调用fs.close是必要的

我在nodejs API中找不到关于fs.close解释的更多信息.我想知道场景调用fs.close是必要的.例如:

var fs =  require('fs');
fs.writeFile("/home/a.tex","abc"); or like fs.appendFile("/home/a.tex","close")
fs.close(); //is it necessary?

如果我不打电话给fs.close有什么影响吗?

任何帮助表示赞赏.

file-io node.js

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

node js:fs.rename覆盖文件(如果已存在)

如果文件已存在,fs.rename是否会覆盖该文件?

var fs = require('fs'),
    oldPath = 'firstfile.txt',
    newPath = 'temp/firstfile.txt';

fs.rename(oldPath, newPath, function (err) {
    console.log('rename callback ', err); 
});
Run Code Online (Sandbox Code Playgroud)

如果'/newFolder/somefile.txt'存在,会发生什么?

file-io node.js

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

新贵:工作未能开始

编辑

status marybaked 产量

marybaked停止/等待

这是输出/var/log/syslog:

5月3日16:24:39 marybakedpdx内核:[3464.189563] init:无法生成marybakedpdx主进程:无法找到setuid用户

5月3日16:24:44 marybakedpdx内核:[3469.342062] init:无法生成marybaked主进程:无法找到setuid用户


当我跑步时,start marybaked我得到:

开始:作业无法启动

当我跑步时,start <anything else>我得到:

开始:未知工作:

我的/var/logs/upstart目录中没有marybaked.log日志......这里发生了什么?upstart如何认识到这marybaked是一项工作并且无法启动它,但是没有为它创建错误日志?

这是我的/etc/init/marybaked.conf档案:

# upstart service file at /etc/init/marybakedpdx.conf
    description "Meteor.js (NodeJS) application"
    author "Daniel Speichert <daniel@speichert.pro>"

    # When to start the service
    start on started mongodb and runlevel [2345]

    # When to stop the service
    stop on shutdown

    # Automatically restart process if crashed
    respawn
    respawn limit 10 5 …
Run Code Online (Sandbox Code Playgroud)

ubuntu nginx upstart meteor

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

10
推荐指数
2
解决办法
9450
查看次数

快速会话滚动不起作用

我看到了很多与此相关的问题,但没有一个答案能解决我的问题。我正在使用express-session,并且只要用户在 cookie MaxAge 过期之前发出请求,我希望用户会话保持活动状态,因此我使用该rolling选项。为了进行测试,我将 MaxAge 过期时间设置为 30 秒。

如果我在两次单击之间仅浏览几秒钟,则会话似乎保持活动状态。但是,如果我发出请求然后等待 20 秒,会话就会过期并且我会被注销,即使我在 30 秒之内。

这是我的express-session配置方式:

var express = require('express')
, cookieParser = require('cookie-parser')
, session = require('express-session');

var app = express();
app.use(cookieParser(EXPRESS_SECRET));
app.use(
    session({
        cookie: { maxAge : 30000 },     //in milliseconds. 30 seconds for testing
        resave: false,                  //Save the session to store even if it hasn't changed
        rolling: true,                  //Reset the cookie Max-Age on every request
        saveUninitialized: false,       //Don't create a session for anonymous …
Run Code Online (Sandbox Code Playgroud)

session node.js express express-session

8
推荐指数
0
解决办法
5179
查看次数

原子更新多个文档并返回它们

在 中MongoDB,我正在寻找一种方法来原子地更新多个文档并在一次调用中返回所有更新的文档。

我们可以在 中执行以下所有操作MongoDB

  • 原子更新一个文档并返回更新后的文档:findAndModify或者findOneAndUpdate
  • 原子更新多个文档:update(...{multi: true}updateMany
  • 查询并返回多个文档:find

我不喜欢一种方法来更新多个文档并在一次调用中将它们全部返回。有办法吗?我用作Mongoose查询包。

atomic mongoose mongodb

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

html div背景图像没有显示出来

我正在网页上工作(我当然是个新手),背景图片不会显示在div中.我尝试了背景图像和背景,它们都不起作用......继承人的代码

如果有人可以帮助那将是伟大的!

<!DOCTYPE html>
<html>
  <head>
    <style type="text/css">
body {background-color:gray;}
</style>
  </head>
<body>
  <div style="background-image: url('/ximages/websiteheader1.png');height:200px;width:1200px;">
  </div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

html css background

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

如何测试node.js websocket服务器?

我正在使用标准配置的sockjs.

   var ws = sockjs.createServer();
    ws.on('connection', function(conn) {
        conn.on('data', function(message) {
            wsParser.parse(conn, message)
        });
        conn.on('close', function() {

        });
    });

    var server = http.createServer(app);
    ws.installHandlers(server, {prefix:'/ws'});
    server.listen(config.server.port, config.server.host);
Run Code Online (Sandbox Code Playgroud)

wsParser.parse 功能是这样的:

function(conn, message) {

(...)

switch(message.action) {
    case "titleAutocomplete":
        titleAutocomplete(conn, message.data);
        break;
    (...) // a lot more of these
 }
Run Code Online (Sandbox Code Playgroud)

在switch中调用的每个方法都会向客户端发送一条消息.

var titleAutocomplete = function(conn, data) {

    redis.hgetall("titles:"+data.query, function(err, titles){
        if(err) ERR(err);

        if(titles) {
            var response = JSON.stringify({"action": "titleAutocomplete", "data": {"titles": titles}});
            conn.write(response);
        } 
    })
};
Run Code Online (Sandbox Code Playgroud)

现在我的问题是我想为我的代码进行测试(比我猜的更好),我不知道该怎么做.我开始用mocha + supertest编写正常的http测试,但我只是不知道如何处理websockets.

我希望只有一个websocket连接可以通过所有测试重用,我在第一个消息之后将websocket连接与用户会话绑定,我也想测试该持久性.

如何利用ws客户端的onmessage事件并在我的测试中使用它?测试如何分辨收到的消息并知道他们应该等待哪一个?

unit-testing mocha.js websocket node.js sockjs

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

如何让 Write-Host 输出 $true

我正在编写一个write-host用于输出另一个 powershell 脚本的 powershell 脚本。如何写出值为 $true(或 $false)的布尔参数,包括美元符号:

param
(
    [switch] $myParam = $true
)

Write-Host My Param is $myParam
Run Code Online (Sandbox Code Playgroud)

我需要这个来准确输出

My Param is $True
Run Code Online (Sandbox Code Playgroud)

但它输出

My Param is True
Run Code Online (Sandbox Code Playgroud)

powershell

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

Sendgrid 总是发送文本电子邮件

我有一个 HTML sendgrid 模板,正在使用 npm 包“sendgrid”通过 Node.js 发送它。问题是我总是以文本格式而不是 HTML 接收电子邮件,即使模板具有 HTML。

代码:

var email = new sendgrid.Email({
    to      : 'me@here.com',
    from    : 'you@there.com',
    subject : 'Saying Hi with HTML Template',
    text    : 'Body'    //This is required
});

email.addFilter('templates', 'enable', 1);
email.addFilter('templates', 'template_id', '12131331.....');
email.addSubstitution('{{TOKEN1}}', 'value');

sendgrid.send(email, function(err, json) {
    if (err) { console.error(err); }
    console.log(json);
});
Run Code Online (Sandbox Code Playgroud)

模板

<html>
<head><title></title></head>
<body>
  <h1>This is a test</h1>
  <p>{{TOKEN1}}</p>
  <p><a href="http://www.there.com">There</a></p>

  <div>&lt;%body%&gt;</div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

是否有我应该设置的代码参数?还是模板本身的设置以允许 HTML?

html email node.js sendgrid

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