如何获取当前模块的目录和文件名?在 Node.js 中,我会使用:__dirname&__filename为此
我有一个socket.io聊天室运行,当我们在一台机器上运行时,其流量变得越来越大.我们使用ws库为套接字运行基准测试,它们的性能要好得多,这样可以更好地利用我们的硬件.这需要重写我们的应用程序.
我们的socket.io应用程序允许用户创建使用命名空间实现的私人聊天室.例如
localhost:8080/room/1
localhost:8080/room/2
localhost:8080/room/3
Run Code Online (Sandbox Code Playgroud)
当一切都在一个实例中时很容易,但现在我们正在寻求将这个容量扩展到多个节点.
我们在亚马逊的云中运行这个实例.以前看起来像缩放websockets是ELB的一个问题.我们注意到Amazon现在支持和支持websockets的应用程序负载均衡器.这听起来不错,但在阅读完文档之后我必须承认我并不知道这意味着什么.如果我使用带有数千个命名空间的socket.io,我只是将实例放在这个ALB后面,一切都会起作用吗?我的主要问题是:
如果x个用户加入命名空间,ALB会自动将我的消息重定向到适当的用户吗?所以假设我在ALB后面运行了5个vanilla socket.io实例.用户1创建名称空间.几个小时后通过,用户99999来了,并希望加入这个命名空间,是否需要编写任何其他代码来执行此操作,或者alb会重定向所有应该去的地方吗?发送和接收消息也是如此?
我试图使用写入文件 Deno.writeFile
await Deno.writeFile('./file.txt', 'some content')
Run Code Online (Sandbox Code Playgroud)
但得到以下神秘错误:
error: Uncaught TypeError: arr.subarray is not a function
at Object.writeAll ($deno$/buffer.ts:212:35)
at Object.writeFile ($deno$/write_file.ts:70:9)
Run Code Online (Sandbox Code Playgroud)
在 Deno 中写入文件的正确方法是什么?
这段代码
const file = require("fs").createWriteStream("./test.dat");
for(var i = 0; i < 1e7; i++){
file.write("a");
}
Run Code Online (Sandbox Code Playgroud)
运行约30秒后出现此错误消息
<--- Last few GCs --->
[47234:0x103001400] 27539 ms: Mark-sweep 1406.1 (1458.4) -> 1406.1 (1458.4) MB, 2641.4 / 0.0 ms allocation failure GC in old space requested
[47234:0x103001400] 29526 ms: Mark-sweep 1406.1 (1458.4) -> 1406.1 (1438.9) MB, 1986.8 / 0.0 ms last resort GC in old spacerequested
[47234:0x103001400] 32154 ms: Mark-sweep 1406.1 (1438.9) -> 1406.1 (1438.9) MB, 2628.3 / 0.0 ms last resort GC in …Run Code Online (Sandbox Code Playgroud) 如何将字符串转换为缓冲区?
我试过了:Uint8Array.from('hello world')但它不起作用
我正在尝试下载一个 10GB 的文件,但只有 4GB 被保存到磁盘,并且内存增长了很多。
const res = await fetch('https://speed.hetzner.de/10GB.bin');
const file = await Deno.open('./10gb.bin', { create: true, write: true })
const ab = new Uint8Array(await res.arrayBuffer())
await Deno.writeAll(file, ab)
Run Code Online (Sandbox Code Playgroud) 我试图在我的快速节点应用程序上注册车把,但似乎不起作用
const express = require('express');
const hbs = require('hbs');
const expressHbs = require('express-handlebars');
const app = express();
app.engine('.hbs', expressHbs({ defaultLayout: 'layout', extname: '.hbs' }));
app.set('view engine', 'hbs');
hbs.registerHelper('if_equal', function(a, b, opts) {
if (a == b) {
return opts.fn(this)
} else {
return opts.inverse(this)
}
});
Run Code Online (Sandbox Code Playgroud)
在 .hbs 文件中,我运行此代码
{{#if_equal x "my_string"}}
x is "my_string"
{{else}}
x isn't "my_string"
{{/if_equal}}
Run Code Online (Sandbox Code Playgroud)
然后我收到了这个错误
错误:缺少助手:“if_equal”把手
我在对话框流中填充插槽/参数时遇到问题。我无法搜索关于如何使用 webhooks/backend-code 进行参数填充的任何好的文档。
我的用例是,我想提取date但如果用户没有提供 YEAR 那么它应该询问用户“哪一年?”。然后填回去date。
我使用$date.partial的value,因此是给UUUU全年的一部分,但我怎么能提示用户给予一年,这样我可以在参数填充得到完整的日期。
任何帮助表示赞赏。
我有一个asyncExpress中间件,因为我想在其中使用中间件await来清理我的代码。
const express = require('express');
const app = express();
app.use(async(req, res, next) => {
await authenticate(req);
next();
});
app.get('/route', async(req, res) => {
const result = await request('http://example.com');
res.end(result);
});
app.use((err, req, res, next) => {
console.error(err);
res
.status(500)
.end('error');
})
app.listen(8080);
Run Code Online (Sandbox Code Playgroud)
问题是当它拒绝时,它不会进入我的错误中间件,但是如果我删除了async关键字,并且throw在中间件内部,它就会去。
app.get('/route', (req, res, next) => {
throw new Error('Error');
res.end(result);
});
Run Code Online (Sandbox Code Playgroud)
所以我得到的UnhandledPromiseRejectionWarning不是输入我的错误处理中间件,而是如何让错误冒泡并表达处理它?
当我运行以下程序时
async function functionOne() {
throw new Error('Error here prints the complete stack');
await new Promise((resolve) => {
setTimeout(() => { resolve(); }, 1000);
});
}
async function functionTwo() {
await functionOne();
}
async function functionThree() {
await functionTwo();
}
functionThree()
.catch((error) => {
console.error(error);
});
Run Code Online (Sandbox Code Playgroud)
我得到以下输出,该输出通过各种调用的函数打印堆栈
Error: Error here prints the complete stack
at functionOne (/home/divyanshu/programming/errorHandlingAsyncAwait/index.js:3:9)
at functionTwo (/home/divyanshu/programming/errorHandlingAsyncAwait/index.js:11:9)
at functionThree (/home/divyanshu/programming/errorHandlingAsyncAwait/index.js:15:9)
at Object.<anonymous> (/home/divyanshu/programming/errorHandlingAsyncAwait/index.js:18:1)
at Module._compile (module.js:612:30)
at Object.Module._extensions..js (module.js:623:10)
at Module.load (module.js:531:32)
at tryModuleLoad (module.js:494:12)
at Function.Module._load …Run Code Online (Sandbox Code Playgroud) node.js ×8
javascript ×5
deno ×4
express ×2
amazon-ec2 ×1
amazon-elb ×1
async-await ×1
chatbot ×1
es6-promise ×1
node-streams ×1
python ×1
socket.io ×1
stack-trace ×1
v8 ×1
websocket ×1