小编Pau*_*kin的帖子

包括Node.js中另一个文件的Javascript类定义

我正在为Node.js编写一个简单的服务器,我正在使用我自己的类User,它看起来像:

function User(socket) {
    this.socket = socket;
    this.nickname = null;

    /* ... just the typical source code like functions, variables and bugs ... */

    this.write = function(object) {
        this.socket.write(JSON.stringify(object));
    }
};
Run Code Online (Sandbox Code Playgroud)

然后在这个过程中我会实例化它:

var server = net.createServer(function (socket) {
    /* other bugs */
    var user = new User(socket);
    /* more bugs and bad practise */
});
Run Code Online (Sandbox Code Playgroud)

我可以将我的User类定义移动到另一个javascript文件并以某种方式"包含"它吗?

javascript node.js

95
推荐指数
4
解决办法
10万
查看次数

为什么 Google Chrome 中的 voiceSynthesis.getVoices() 返回空列表?

我想要访问桌面 Chrome、Safari 和 Firefox 上 SpeechSynthesis API 的一部分的语音列表。如果我在每个浏览器中打开一个新选项卡,然后通过控制台运行:

speechSynthesis.getVoices()
Run Code Online (Sandbox Code Playgroud)

...我期望返回一个包含“SpeechSynthesisVoice”对象(即可用语音)的数组。Firefox 和 Safari 的行为符合预期,但在 Chrome 中,第一次调用 getVoices() 返回一个空数组。我必须再次调用该方法才能接收预期的填充数组。

为什么 Chrome 会有这样的表现?它是否对某些 Web API 进行某种延迟加载?请帮助我理解。

javascript dom google-chrome speech

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

如何使用 GraphQL 处理 long Int?

如您所知,GraphQL 没有像 long int 这样的数据类型。因此,每当数字像 一样大时10000000000,它就会抛出这样的错误:Int cannot represent non 32-bit signed integer value: 1000000000000

为此,我知道两种解决方案:

  1. 使用标量。
import { GraphQLScalarType } from 'graphql';
import { makeExecutableSchema } from '@graphql-tools/schema';

const myCustomScalarType = new GraphQLScalarType({
  name: 'MyCustomScalar',
  description: 'Description of my custom scalar type',
  serialize(value) {
    let result;
    return result;
  },
  parseValue(value) {
    let result;
    return result;
  },
  parseLiteral(ast) {
    switch (ast.kind) {
    }
  }
});

const schemaString = `

scalar MyCustomScalar

type Foo {
  aField: MyCustomScalar
}

type …
Run Code Online (Sandbox Code Playgroud)

javascript bigint node.js graphql

6
推荐指数
2
解决办法
9798
查看次数

用curl调用golang调用jsonrpc

我在golang上写了"hello world"rpc服务.它运行正常,并且jsonrpc客户端正在运行.但是我需要用curl发送请求,这个例子不起作用:

curl \
-X POST \
-H "Content-Type: application/json" \
-d '{"id": 1, "method": "Test.Say", "params": [{"greet": "world"}]}' \
http://localhost:1999/_goRPC_
Run Code Online (Sandbox Code Playgroud)

去接受连接,但绝对没有结果:

curl: (52) Empty reply from server 
Run Code Online (Sandbox Code Playgroud)

这是我的代码:

package main

import (
  "log"
  "os"
  "time"
  "net"
  "net/rpc"
  "net/rpc/jsonrpc"
)

// RPC Api structure
type Test struct {}

// Greet method arguments
type GreetArgs struct {
  Name string
}

// Grret message accept object with single param Name
func (test *Test) Greet(args *GreetArgs, result *string) (error) {
  *result = "Hello …
Run Code Online (Sandbox Code Playgroud)

curl http json-rpc go

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

node.js Nodemailer 对象数组到 CSV 文件作为电子邮件中的附件

我正在使用 nodemailer 模块在 node.js 中发送电子邮件。我有一组对象,我试图将它们转换为 CSV 文件并将其附加到电子邮件中。我能够成功发送电子邮件,但 CSV 附件未正确填充(显示为空白)。我的代码类似于以下内容

var nodemailer = require('nodemailer');

// create reusable transporter object using SMTP transport
    var transporter = nodemailer.createTransport({
    service: 'Gmail',
    auth: {
        user: 'gmail.user@gmail.com',
        pass: 'userpass'
     }
});

var data = [{age:24,name:"bob"},{age:35,name:"andy"},...];

var mailOptions = {
    from: 'foo@blurdybloop.com', // sender address
    to: 'bar@blurdybloop.com', //receiver
    subject: 'Hello', // Subject line
    text: 'Hello world', // plaintext body
    html: '<b>Hello world</b>', // html body
    attachments: [{   
        filename: 'test.csv',
        content: data 
    }],
};

// send mail with …
Run Code Online (Sandbox Code Playgroud)

javascript csv node.js nodemailer

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

避免输入溢出容器并在调整大小时让它们跳到下一行

当我添加太多输入(取决于我想要多少)时,它们会溢出它们的容器,例如当我只有两个输入时,它们会在我调整窗口大小时溢出容器。

这是我的 html 和 css:

#datails_container {
    position: relative;
    display: flex;
    background-color: darkgrey;
    width: 70%;
    height: 550px;
    border-radius: 5px;
    top: 50px;
    bottom: 0;
    left: 0;
    right: 0;
    margin: auto;
    background-color: black;
    border-radius: 6px;
}

.theight {
    height: 26%;
    border: 1px black solid;
    margin: auto;
    display: flex;
    width: 80%;
    background-color: white;
    border-radius: 4px;
}

form {
    width: 100%;
    height: 100%;
    align-items: center;
    display: flex;
    flex-flow: row wrap;
    justify-content: space-between;
}

span {
    background-color: #b50612;
    width: 92px;
    height: min-content;
    border-radius: 5px;
    position: …
Run Code Online (Sandbox Code Playgroud)

html javascript css

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

标签 统计

javascript ×5

node.js ×3

bigint ×1

css ×1

csv ×1

curl ×1

dom ×1

go ×1

google-chrome ×1

graphql ×1

html ×1

http ×1

json-rpc ×1

nodemailer ×1

speech ×1