我试图将请求的二进制体写入文件并失败.该文件是在服务器上创建的,但我无法打开它.我在Ubuntu上收到'致命错误:不是png'.以下是我提出请求的方式:
curl --request POST --data-binary "@abc.png" 192.168.1.38:8080
Run Code Online (Sandbox Code Playgroud)
以下是我试图用文件保存它的方法.第一个片段是用于将所有数据附加在一起的中间件,第二个是请求处理程序:
中间件:
app.use(function(req, res, next) {
req.rawBody = '';
req.setEncoding('utf-8');
req.on('data', function(chunk) {
req.rawBody += chunk;
});
req.on('end', function() {
next();
});
});
Run Code Online (Sandbox Code Playgroud)
处理器:
exports.save_image = function (req, res) {
fs.writeFile("./1.png", req.rawBody, function(err) {
if(err) {
console.log(err);
} else {
console.log("The file was saved!");
}
});
res.writeHead(200);
res.end('OK\n');
};
Run Code Online (Sandbox Code Playgroud)
这里有一些可能有用的信息.在中间件中,如果我记录rawBody的长度,它看起来是正确的.我真的很困惑如何正确保存文件.我所需要的只是朝着正确的方向努力.
给定以下文件:
主机
127.0.0.1 localhost
Run Code Online (Sandbox Code Playgroud)
项目-a.hosts
127.0.0.1 project-a
Run Code Online (Sandbox Code Playgroud)
项目-b.hosts
127.0.0.1 project-b
Run Code Online (Sandbox Code Playgroud)
通过 Node 中的 FS 将主机文件内容替换为另一个给定文件的最简单方法是什么?
我想删除某个目录中文件名以相同字符串开头的所有文件,例如我有以下目录:
public/
profile-photo-SDS@we3.png
profile-photo-KLs@dh5.png
profile-photo-LSd@sd0.png
cover-photo-KAS@hu9.png
Run Code Online (Sandbox Code Playgroud)
所以我想应用一个函数来删除以字符串开头的所有文件,profile-photo以使其末尾具有以下目录:
public/
cover-photo-KAS@hu9.png
Run Code Online (Sandbox Code Playgroud)
我正在寻找这样的功能:
fs.unlink(path, prefix , (err) => {
});
Run Code Online (Sandbox Code Playgroud) 我有一个正在从音频源读取的流,我正在尝试将其存储到Buffer. 从我读过的文档来看,您可以使用而不是文件路径将pipe流传输到一个流中。fs.createWriteStream(~buffer~)
我目前正在这样做:
const outputBuffer = Buffer.alloc(150000)
const stream = fs.createWriteStream(outputBuffer)
Run Code Online (Sandbox Code Playgroud)
但是当我运行它时,它会抛出一个错误,指出Path: must be a string without null bytes文件系统调用。
如果我误解了文档或遗漏了一些明显的内容,请告诉我!
如何使数据按调用方式写入文件WriteStream.write()?
编辑:事实证明,当我使用 REPL 并调用该函数时,这是有效的。但是,这在我的程序中不起作用:
import * as FS from "fs";
import { LetterGroup } from "./LetterGroup";
import { Dictionary } from "./Dictionary";
import { Word } from "./Word";
import * as OS from "os";
const groups: Array<LetterGroup> = LetterGroup.letterGroupsFromString(FS.readFileSync("./letterGroups.txt").toString());
const dictionary = Dictionary.create(FS.readFileSync("./dictionary.txt").toString());
const inputStr: string = FS.readFileSync("./input.txt").toString();
const inputWords = new Array<Word>();
const fileStream = FS.createWriteStream("./output.txt");
for (const line of inputStr.trim().split(OS.EOL))
{
inputWords.push(new Word(line));
}
function permute(index: number)
{
index = Math.floor(index);
if (!(index >= 0 && …Run Code Online (Sandbox Code Playgroud) 在使用节点脚本进行一些搜索替换后,我试图重命名一个文件夹(WordPress 主题),但该文件夹的重命名似乎失败了。
我想要这个
public_html/wp-content/my_theme/
Run Code Online (Sandbox Code Playgroud)
成为
public_html/wp-content/something_other/
Run Code Online (Sandbox Code Playgroud)
文件夹的名称取自提示(这部分有效,因为文件中的搜索替换工作正常)。
脚本看起来像这样
const fs = require('fs');
const path = require('path');
const rootDir = path.join(__dirname, '..');
// themePackageName is taken from the prompt and is defined
if (themePackageName !== 'my_theme') {
fs.renameSync(`${rootDir}/wp-content/my_theme/`, `${rootDir}/wp-content/${themePackageName}/`, (err) => {
if (err) {
throw err;
}
fs.statSync(`${rootDir}/wp-content/${themePackageName}/`, (error, stats) => {
if (error) {
throw error;
}
console.log(`stats: ${JSON.stringify(stats)}`);
});
});
}
Run Code Online (Sandbox Code Playgroud)
这基本上是从这里获取的
我收到的错误是
fs.js:781
return binding.rename(pathModule.toNamespacedPath(oldPath),
^
Error: ENOENT: no such file or directory, rename '/vagrant-local/www/me/wp-boilerplate/public_html/wp-content/my_theme/' -> …Run Code Online (Sandbox Code Playgroud) 我在音频处理时将 Int16Array 缓冲区发送到服务器
var handleSuccess = function (stream) {
globalStream = stream;
input = context.createMediaStreamSource(stream);
input.connect(processor);
processor.onaudioprocess = function (e) {
var left = e.inputBuffer.getChannelData(0);
var left16 = convertFloat32ToInt16(left);
socket.emit('binaryData', left16);
};
};
navigator.mediaDevices.getUserMedia(constraints)
.then(handleSuccess);
Run Code Online (Sandbox Code Playgroud)
在服务器中我尝试保存文件如下
client.on('start-audio', function (data) {
stream = fs.createWriteStream('tesfile.wav');
});
client.on('end-audio', function (data) {
if (stream) {
stream.end();
}
stream = null;
});
client.on('binaryData', function (data) {
if (stream !== null) {
stream.write(data);
}
});
Run Code Online (Sandbox Code Playgroud)
但这不起作用,那么我如何将此数组缓冲区保存为 wav 文件?
我正在尝试创建一个小型构建脚本,如果在默认路径中找不到 mysql 标头,该脚本将询问用户 mysql 标头的位置。现在我用来inquirer提示用户输入,效果很好,但我遇到了以下问题:
'use strict'
const inquirer = require('inquirer')
const fs = require('fs')
const MYSQL_INCLUDE_DIR = '/usr/include/mysql'
let questions = [
{
type: 'input',
name: 'MYSQL_INCLUDE_DIR',
message: 'Enter path to mysql headers',
default: MYSQL_INCLUDE_DIR,
when: (answers) => {
return !fs.existsSync(MYSQL_INCLUDE_DIR)
},
validate: (path) => {
return fs.existsSync(path)
}
}
]
inquirer.prompt(questions)
.then((answers) => {
// Problem is that answers.MYSQL_INCLUDE_DIR might be undefined at this point.
})
Run Code Online (Sandbox Code Playgroud)
如果找到 mysql 标头的默认路径,则不会显示问题,因此不会设置答案。如何设置问题的默认值而不实际向用户显示它?
解决上述问题也可以做到这一点,而不是使用全局变量:
let questions = [
{
type: …Run Code Online (Sandbox Code Playgroud) 我正在尝试第 3 方 api 的多重导入功能,其中我需要传递 json 文件读取流。
但我的 api 中已经有一组用户数据,我需要将其作为读取流传递。
我已经尝试过的选项。
fs.createReadStreamfs.createReadStream(Buffer.from(JSON.stringify('[{ user: "data"}]')));
createReadStream 不接受 Buffer 并返回
ENOENT: 没有那个文件或目录,打开
require('stream').Readable
const Readable = require('stream').Readable;
const readStream = new Readable();
readStream._read = () => {};
readStream.push(JSON.stringify(u));
readStream.push(null);
读取流在我的请求中给出“无效的多部分有效负载格式”错误
请找到我为多用户文件上传传递的请求对象
{
"url": "",
"method": "POST",
"headers": {},
"formData": {
"userFile": ReadStream
}
}
Run Code Online (Sandbox Code Playgroud)
任何建议将不胜感激
我无法将视频等大文件上传到 s3。它最终会超时。我尝试使用 fs 来传输它,但我一定没有正确使用它。
我已经尝试了所有我能想到的方法来让 fs 流式传输该文件。我不知道是否可以像我在单独的上传路径中使用 multerS3 那样使用 fs 。我可以上传图像和非常小的视频,但仅此而已。
// Here is my s3 index file which exports upload
const crypto = require('crypto');
const aws = require('aws-sdk');
const multerS3 = require('multer-s3');
const fs = require('fs');
aws.config.update({
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
region: 'us-east-1',
ACL: 'public-read'
});
const s3 = new aws.S3({ httpOptions: { timeout: 10 * 60 * 1000 }});
var options = { partSize: 5 * 1024 * 1024, queueSize: 10 };
const fileFilter = (req, file, cb) …Run Code Online (Sandbox Code Playgroud) fs ×10
node.js ×10
javascript ×4
buffer ×2
file ×2
filesystems ×2
amazon-s3 ×1
binary-data ×1
file-upload ×1
inquirer ×1
multer-s3 ×1
node-modules ×1
node-streams ×1
unlink ×1
wav ×1