小编use*_*178的帖子

如何从 execSync() 获取 err、stderr?

在节点12中,execSync可以返回stdout,例如

const execSync = require('child_process').execSync;

const stdout = execSync('ls');
console.log(`stdout: ${stdout}`);
Run Code Online (Sandbox Code Playgroud)

但是如何获得 err 和 stderr 呢?在 child_process.exec 的回调中,您拥有全部 3 个。

我可以使用异步方式,但更喜欢使用 execSync() 如果更容易的话。

node.js

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

node.js如何通过进程名称检查进程是否正在运行?

我在linux中运行node.js,从node.js如何检查进程是否从进程名运行?最糟糕的情况我使用child_process,但想知道是否有更好的方法?

谢谢 !

node.js

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

节点Multer内存存储:如何释放内存

Ok multer支持内存存储,这很棒.

  1. 但是如何在上传文件后释放内存?

    storage = multer.memoryStorage();

    upload = multer({storage:storage}).single('image');

对于磁盘存储,我可以删除dest文件夹中的文件.

  1. 如果2个用户上传具有相同文件名的文件会发生什么情况,内存存储会发生什么?

谢谢 !

node.js multer

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

Passport isAuthenticated()始终返回TRUE

使用护照本地认证,登录工作,我点击getStatus按钮,它工作,然后注销工作.但是在注销后,我在浏览器中单击BACK,它仍然可以显示完整的内容getStatus.控制台登录isAuthenticated()仍然显示"您已登录".这是简化的代码:

var express    = require('express');
var passport   = require('passport');
var net        = require('net');
var bodyParser = require('body-parser');
var http       = require('http');
var multer     = require('multer');
var cp         = require('child_process');
var exec       = require('child_process').exec;
var sys        = require('sys');
var path       = require('path');
var util       = require('util');
var session    = require('express-session');

var crypto     = require('crypto');
var sqlite3    = require('sqlite3');

/////////////////////////////////////////////////
var LocalStrategy = require('passport-local').Strategy;
var db = new sqlite3.Database('./myPassword.db');

passport.use(new LocalStrategy(function(username, password, done)
{
    console.log("step 2: …
Run Code Online (Sandbox Code Playgroud)

node.js passport.js

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

如何在中间件中等待 next()

使用express,我有一个像这样的中间件:

app.use((req, res, next) => {
  console.log(req);
  next();
});
Run Code Online (Sandbox Code Playgroud)

如何等待 next() 完成?我问的原因是我在 next() 之后添加了 console.log,此消息发生在下一个路由函数中的消息之前。

app.use(async(req, res, next) => {
  console.log(req);
  await next();
  console.log('should be the last print but ...');
});
Run Code Online (Sandbox Code Playgroud)

node.js express

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

如何从 npm 脚本等待服务器启动完成?

我正在尝试为节点应用程序运行功能测试。

在我的package.json我有以下脚本:

"scripts": {
  "web-server": "NODE_ENV=test node app.js &",
  "test": "npm run web-server && mocha ./tests/functional/*.js --exit",
  "posttest": "pkill -f node"
}
Run Code Online (Sandbox Code Playgroud)

但是在运行它时,测试会在服务器完成启动之前运行。

如何等待服务器?

race-condition node.js

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

Node.js:如何使默认页面成为…… 除了index.html

最初,我的node.js服务器index.html默认为。现在,我想默认为默认设置,login.html以便人们可以首先登录。

我的代码是.../server/server.js,当客户端页面都在.../client/login.htmlindex.html

现在我修改server.js为:

app.get('/', function(req, res)
{
    res.sendfile(path.resolve('../client/login.html'));
});
Run Code Online (Sandbox Code Playgroud)

重新启动server.js之后index.html,默认情况下仍指向该网页。我想念什么?

node.js

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

端口8081在"react-native run-ios"中已经在使用

是的,我已经读过这个,但仍然不知道如何使它工作. 本机响应 - 端口8081已在使用中,打包器未运行或未正常运行Command/bin/sh失败,退出代码为2

最初我是这样做的

1. react-native init Hello
2. react-native run-ios
Run Code Online (Sandbox Code Playgroud)

然后点击"已使用的端口8081".

读过反应文档,https://facebook.github.io/react-native/docs/troubleshooting.html

1. cannot kill the process using 8081, it keeps on coming back,
   and i don't want to kill it
2. react-native start --port=8088
3. update node_modules/react-
native/React/React.xcodeproj/project.pbxproj file. Did it.
Run Code Online (Sandbox Code Playgroud)

应该在哪里和哪一步运行"react-native start --port = 8088"?

顺便说一句,我是一名普通的工程师,但如果我不能在2小时内运行第一反应本机样本,我就看不出它是如何飞行的,这非常烦人.

react-native

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

async.waterfall 和 child_process.execSync 有什么区别?

假设这是 linux shell,我想做的是:

copy file1 tmp
rename tmp file2
Run Code Online (Sandbox Code Playgroud)

我可以做瀑布

function copyFile(cb) {
    child_process.exec('cp file1 tmp', function (error, stdout, stderr) {
        ......
    });
}
async.waterfall([
    copyFile,
    renameFile
], function (error) {
    if (error) {
        //handle readFile error or processFile error here
    }
});
Run Code Online (Sandbox Code Playgroud)

或者猜我可以做

child_process.execSync('cp file1 tmp");
child_process.execSync('rename tmp file2');
Run Code Online (Sandbox Code Playgroud)

请问有什么区别?例如性能?阻塞?非常感谢 !

javascript node.js

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

如何在Koa 2中有条件地重定向某些URL

这就是我在想的伪代码。

const myRedirect = (routePath) => {
    newUrl = routePath;
    if (matches condition)
        newUrl = do_some_modification(routePath);       
    return next(newUrl); 
}

const myFunc = (routePath, myRedirect) => (newUrl, middleware) => {
    return (ctx, newUrl, next) => {
        return middleware(ctx, newUrl, next);
    }
};
Run Code Online (Sandbox Code Playgroud)

如何对其进行修改以使其正常工作?

koa koa2

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