刚开始使用Node.js. 在我的app/js文件中,我正在做这样的事情:
app.js
var http = require('http');
http.createServer(function (request, response) {
response.writeHead(200, {'Content-Type': 'text/plain'});
response.end('Am I really running a server?!');
}).listen(8080, '127.0.0.1');
console.log('running server!');
Run Code Online (Sandbox Code Playgroud)
当我在我的终端并运行时node app.js,控制台吐出'running server!',但在我的浏览器中,我得到了,Uncaught ReferenceError: require is not defined.
有人可以向我解释为什么在终端中,它可以正常工作,但在浏览器中,它不是吗?
我正在使用节点http-server来为我的页面提供服务.
我正在编写一个Web应用程序,生成一个用户可以下载的潜在大文本文件,所有处理都在浏览器中完成.到目前为止,我能够以小块的形式读取超过1 GB的文件,处理每个块,逐步生成大量输出文件,并将增长的输出存储在IndexedDB中.我更天真的尝试将所有结果保存在内存中,然后在最后将它们序列化为文件,导致所有浏览器崩溃.
我的问题是双重的:
我可以追加到IndexedDB中的条目(字符串或数组)而不先将整个内容读入内存吗?现在,这个:
task.dbInputWriteQueue.push(output);
var transaction = db.transaction("files", "readwrite");
var objectStore = transaction.objectStore("files");
var request = objectStore.get(file.id);
request.onsuccess = function()
{
request.results += nextPartOfOutput
objectStore.put(request.results);
};
Run Code Online (Sandbox Code Playgroud)
输出开始变大后导致崩溃.我可以在数据库中写一堆小条目,但是后来我不得不将它们全部读入内存,以便将它们连接起来.参见我的问题的第2部分......
我可以创建一个数据对象URL来引用IndexedDB中的值而不将该值加载到内存中吗?对于小字符串我可以做:
var url = window.URL.createObjectURL(new Blob([myString]), {type: 'text/plain'});
Run Code Online (Sandbox Code Playgroud)
但对于大字符串,这并不太好.实际上,它在加载字符串之前崩溃了.似乎使用get()IndexedDB的大读取导致Chrome至少崩溃(甚至开发人员工具崩溃).
如果我使用Blob而不是字符串会更快吗?这种转换便宜吗?
基本上我需要一种方法,使用JavaScript,将一个非常大的文件写入磁盘,而不是在任何一点将整个内容加载到内存中.我知道你可以提供createObjectURL一个文件,但这在我的情况下不起作用,因为我正在从用户提供的文件中生成一个新文件.
例如,我有一个.JSON文件具有以下内容:
[{"honda": "accord", "color": "red"},{"ford": "focus", "color": "black"}]
Run Code Online (Sandbox Code Playgroud)
什么是将另一个对象推{"nissan": "sentra", "color": "green"}入此.json数组以使.json文件看起来像的javascript代码
[{"honda": "accord", "color": "red"},{"ford": "focus", "color": "black"},{"nissan": "sentra", "color": "green"}]
Run Code Online (Sandbox Code Playgroud)
我问的原因是我在网上找到了很多关于如何使用AJAX从.json文件中提取数据但是没有使用AJAX将新数据写入.json文件以使用其他数据更新.json文件的信息.
任何帮助,将不胜感激!谢谢.
我有一个子进程,我在node.js中使用如下.我没有将输出重定向到控制台,而是将输出放在一个位于运行的机器上的某个日志文件中(并且应该适用于windows和mac).
下面的代码是我正在使用的,我想将文件输出到日志文件中.这需要做哪些改变?谢谢!
我的代码:
var spawn = require('child_process').spawn,
ls = spawn('ls', ['-lh', '/usr']);
ls.stdout.on('data', function (data) {
console.log('stdout: ' + data);
});
ls.stderr.on('data', function (data) {
console.log('stderr: ' + data);
});
ls.on('close', function (code) {
console.log('child process exited with code ' + code);
});
Run Code Online (Sandbox Code Playgroud) 我有另一个问题(最后一个问题).目前我正在开发一个Node.js项目,在这个项目中我有很多 console.log()函数.到目前为止,这一切都运行正常,但我也希望写入控制台的所有内容也都写在日志文件中.有人可以帮帮我吗?
例如:
Console.log('The value of array position [5] is '+ array[5]);
Run Code Online (Sandbox Code Playgroud)
在我的真实代码中它更多但这应该给你一个想法.
谢谢你.
在Node.js中,我想读取一个文件,然后console.log()将文件的每一行分开\n.我怎样才能做到这一点?
目前,我有以下代码块:
net = require('net');
var clients = [];
net.createServer(function(s) {
clients.push(s);
s.on('data', function (data) {
clients.forEach(function(c) {
c.write(data);
});
process.stdout.write(data);//write data to command window
});
s.on('end', function() {
process.stdout.write("lost connection");
});
}).listen(9876);
Run Code Online (Sandbox Code Playgroud)
用于将我的Windows计算机设置为服务器并从我的Linux计算机接收数据.它当前正在将数据写入命令窗口.我想将数据写入文本文件到特定位置,我该怎么做?
我正在使用Electron构建应用程序.在这个应用程序中,我正在使用JSON构建数据结构.我的数据结构如下所示:
{
items: [
{ id:1, name:'football' },
{ id:2, name:'soccer ball' },
{ id:3, name:'basketball' }
]
}
Run Code Online (Sandbox Code Playgroud)
我想将此JSON保存到名为"data.json"的文件中.我想将它保存到文件中,因为我想在下次启动应用程序时加载它.我的挑战是,我不知道如何保存数据.事实上,我不确定我应该在哪里保存文件.我是否将其保存在与应用程序相同的目录中?或者我应该使用一些跨平台的方法吗?
目前,我有以下内容:
saveClick: function() {
var json = JSON.stringify(this.data);
// assume json matches the JSON provided above.
// Now, I'm not sure how to actually save the file.
}
Run Code Online (Sandbox Code Playgroud)
那么,我如何/在哪里将JSON保存到本地文件系统以便以后使用?
给定以下文件:
主机
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 将主机文件内容替换为另一个给定文件的最简单方法是什么?
如何使数据按调用方式写入文件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) 我试图上传文件并移动到已经存在的新目录。按照在 Node.js 中写入文件,但出现错误:
Error: EISDIR, open '/Users/name/Sites/project/app/assets/images/UploadTemporary/'
at Error (native)
我发现使用 Node.js 我得到了“错误:EISDIR,读取”和Node.js 错误:EISDIR,打开错误类似的错误消息,我的UploadTemporary文件夹已经存在我搞砸了什么吗?我不明白,如果它不是目录,还有什么可以是?
var multipart = require('connect-multiparty');
var fs = require('fs');
var path = require('path');
var appDir = path.dirname(require.main.filename);
...
var sourceFile = req.files.file[0].path;
var destinationFile = appDir + '/assets/images/UploadTemporary/';
var source = fs.createReadStream(sourceFile);
var destination = fs.createWriteStream(destinationFile);
source.pipe(destination);
source.on('end', function () {
fs.unlinkSync(sourceFile);
});
Run Code Online (Sandbox Code Playgroud) 我正在尝试从node.js写一个文本文件,其中的内容将逐行计算,因此构建一个用于一次写入的字符串将需要二次时间,并且逐行写入似乎是最佳选择.
基本上我正在尝试做一些事情:
FILE *f = fopen("foo.txt", "w");
for (int i = 0; i < 100; i++)
fprintf(f, "line %d\n", i);
Run Code Online (Sandbox Code Playgroud)
node.js的等价物是什么?
如何将字符串数组保存到Node.js 中的JSON文件中:
const alphabet = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'];
Run Code Online (Sandbox Code Playgroud)
例子.json
[
"a",
"b",
"c",
"d",
"e",
"f",
"g",
"h"
]
Run Code Online (Sandbox Code Playgroud) node.js ×10
javascript ×9
file ×4
fs ×4
json ×3
file-io ×2
filesystems ×2
ajax ×1
arrays ×1
bloburls ×1
console.log ×1
electron ×1
indexeddb ×1
logfile ×1
object ×1