node.js

When use vm2 in worker_threads, is it possible to share a NodeVM instance between workers?

本小妞迷上赌 提交于 2021-02-20 00:25:07
问题 I am using worker_threads and vm2 to implement a serverless-like thing, but I cannot get a NodeVM instance in the main thread and then pass through workData(because of worker_threads 's limitation), so I can only new NodeVM in a worker thread per request, inside which I cannot reuse a vm instance and the cost hurts. The new NodeVM() takes 200 ~ 450 ms to finish, so I wish to pre-init a reusable instance. const w = new Worker(` (async () => { const { workerData, parentPort } = require('worker

通过gitee管理hexo管理发布内容

风格不统一 提交于 2021-02-19 22:10:06
想法 最原始的办法是通过ftp或者winscp这样的工具能实现内容上传,但是不够简化,首先你要登录服务器,然后找到相应文件夹进行更新。 第二种方法是先更新gitee仓库,然后通过ssh登录服务器,然后从仓库pull下所有代码 第三种方法是不需要gitee仓库,直接在服务器建一个空仓库,然后把本地发布文件push到服务器仓库上 上面3种方法,第1种最原始,第2种比较繁琐,第3种简单可操作,但是第3种有一个问题,就是代码管理全部在服务器上,可视性比较差,能不能通过gitee管理代码,包括原始文件,然后一旦用户push,通过钩子函数,触发服务器主动拉取,这样,所有操作都简化为了本地的一个git push指令。说干就干,问了一圈度娘,果然高手在民间,几个关键步骤都有相应的帖子介绍。 第一步 建立hexo空仓库 本地把项目文件统统push上去,除了node_module文件夹。这样,项目文件也不会丢失,便于以后复用。 第二步 建立服务器到hexo仓库私钥通信 1. 创建ssh连接密钥 参看 https://gitee.com/help/articles/4181#article-header0 none ssh-keygen -t rsa -C "xxxxx@xxxxx.com" # Generating public/private rsa key pair... 产生两个文件:id

Hot reloading with express and chokidar causes a http headers sent error when using multiple routes

只愿长相守 提交于 2021-02-19 15:54:59
问题 I've been trying a variety of setups for hot reloading and one that I've come across is the https://github.com/glenjamin/ultimate-hot-reloading-example/. Modifying this boilerplate code as a starting point, I've come across the following problem in my server code: // server.js import chokidar from 'chokidar'; import express from 'express'; const app = express(); // this is the middleware for handline all of my routes app.use(function (req, res, next) { require('./server/index')(req, res, next

据说程序员最怕命名!这个 6300 Star 的手册能帮上忙

安稳与你 提交于 2021-02-19 12:07:26
【导语】:naming-cheatsheet 是一个命名备忘录,记录命名的一些常见规范和约定。 简介 在编程工作中,命名是一件让很多开发者都头疼的事情。国外曾经有个一次关于程序员最难任务的投票调查,结果命名占了 49%。 一个好的变量或函数命名,应该能起到自解释的作用,甚至能减少我们代码的注释。 naming-cheatsheet是一个命名备忘录,记录一些常见的规范约定,并提供简单的例子说明。如果能够严格遵守这些规范,相信我们的代码可读性会大大提升,下面就来介绍 naming-cheatsheet 提供的一些建议。 项目地址: https://github.com/kettanaito/naming-cheatsheet 使用英语 这是最基本的一条规则了,英语是编程中的主要语言,所有编程语言的语法都是用英语编写的,通过英语编写代码,可以大大提高其通用性。对于我们国内开发者来说,一定要避免拼音甚至是直接的中文命名。 /* Bad */ const primerNombre = 'Gustavo' const amigos = ['Kate', 'John'] /* Good */ const firstName = 'Gustavo' const friends = ['Kate', 'John'] 命名风格 选择一种命名的风格,并且严格遵守,可以是camelCase,或者snake

Fetch image in React from Node.js request

帅比萌擦擦* 提交于 2021-02-19 08:56:14
问题 When needed, React asks for an image using Superagent : exports.getImgFromSection=function(id,request){ request .get('/img/'+id) .end((error, response) => { if (!error && response) { console.log(response.file); } else { console.log('There was an error fetching server', error); } }) } Node.js answers this way : app.get("/img/:id",function(req, res){ // check if exists and then res.sendFile(path.join(__dirname, './data/img/'+req.params['id']+'.png')); }); But I don't know how to get the image

How to execute Powershell script function with arguments using Node JS?

给你一囗甜甜゛ 提交于 2021-02-19 08:52:33
问题 I have one powershell script which has multiple function and one function is calling another function. I want to execute that function with some argument using node js. I am using node-powershell npm, but it executes only powershell command not a function. I have done this with C#, but in node js i am not able to do this. C# code to run powershell function: using (var runspace = RunspaceFactory.CreateRunspace()) { try { log.Info(domain+"," + devicetype + "," + fullpath + "," + computername +

Require (the same instance of) socket.io in multiple modules

假如想象 提交于 2021-02-19 08:42:06
问题 I am a bit confused, about how to require and use modules in Node.js. My scenario is the following: I wrote a complete server in one single file, which uses Socket.io for realtime communication. Now the index.js became pretty big, and I want to split the code into several modules to make it more managable. For example I have some functions for serving a Survey to the clients, and getting back their answers. I put all those functions in a seperate module, and require it in the index.js. Works

How do I effectively send a large packet / combine smaller packets?

☆樱花仙子☆ 提交于 2021-02-19 08:28:22
问题 I have a larger buffer I'm trying to send as a packet. Nodejs splits the buffer into smaller (65k packets). Once they are received by the client, how can I ensure the packets go together and effectively recombine them into a buffer? Pretty much using this as a test: // tcp socket var buf = Buffer.alloc(265000); socket.write(buf); Then on client side I need to combine the 65k packets somehow together back into a buffer. Thanks 回答1: TCP is free to break data up on the wire into packets of any

Event Queuing in NodeJS

泄露秘密 提交于 2021-02-19 08:26:18
问题 NodeJS uses a event driven model in which only one thread executes the events. I understand the first event executed will be the user JS code. Simple example from nodeJS website of a webserver is below var http = require('http'); http.createServer(function (req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello World\n'); }).listen(1337, '127.0.0.1'); console.log('Server running at http://127.0.0.1:1337/'); First event executed will perform the above steps. Later on

page-break not working in html-pdf coverter in nodejs

纵饮孤独 提交于 2021-02-19 08:18:05
问题 I am using html-pdf 2.2.0 module to convert my html in pdf. I am using table with multiple rows so I want to use page-break so then a single row does not divide in two pages but its not working when converting it to pdf. Below is the code sample <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> </head> <style> table tbody tr { height