node.js

原生js实现base64编码与解码字符串及对象

╄→гoц情女王★ 提交于 2021-02-09 09:51:35
前言:最近维护了node开发jade模版的项目,场面一度尴尬,和平时做的大相径庭,好不难受。特别是有需要把参数转换为base64拼接到url上,各种姿势试了,只能用原生方法编码解码,来总结一下。 一. Window 中的 btoa()和atob()解决字符串 该编码和解码只实用于字符串。btoa()该方法使用 "A-Z", "a-z", "0-9", "+", "/" 和 "=" 字符来编码字符串,返回一个 base-64 编码的字符串;atob() 用于解码。使用方法:`编码:const newBase = window.btoa("test");解码:const oldValue = window.atob(newBase);`。 注:如果有中文,需要使用URL转码配合使用。使用方法:`编码:const newBase = window.btoa(window.encodeURIComponent(JSON.stringify("原生js实现base64编码与解码字符串及对象")));解码:const oldValue =window.decodeURIComponent(window.atob(newBase));` 二. Node.js Buffer(缓冲区)解决对象 JavaScript 语言自身只有字符串数据类型,没有二进制数据类型。但在处理像TCP流或文件流时

wkhtmltopdf: No such file or directory [ Closed ]

痞子三分冷 提交于 2021-02-09 09:18:21
问题 I installed wkhtmltopdf from the following code sudo apt-get install wkhtmltopdf But when I am creating PDF then it is generating following error Error: /bin/bash: /usr/bin/wkhtmltopdf: No such file or directory 回答1: It appears that when you're trying to run wkhtmltopdf, it's not finding the program to execute. You can try to locate it with locate wkhtmltopdf . That should return the path to the executable. If that doesn't return a path, you can use this (but it will take longer): find /

wkhtmltopdf: No such file or directory [ Closed ]

倖福魔咒の 提交于 2021-02-09 09:16:20
问题 I installed wkhtmltopdf from the following code sudo apt-get install wkhtmltopdf But when I am creating PDF then it is generating following error Error: /bin/bash: /usr/bin/wkhtmltopdf: No such file or directory 回答1: It appears that when you're trying to run wkhtmltopdf, it's not finding the program to execute. You can try to locate it with locate wkhtmltopdf . That should return the path to the executable. If that doesn't return a path, you can use this (but it will take longer): find /

discord.js v12: How do I await for messages in a DM channel?

江枫思渺然 提交于 2021-02-09 09:13:14
问题 This is the code that I've tried: message.author.dmChannel.awaitMessages(msg => { console.log(msg.content) }); But it returns this error message: TypeError: Cannot read property 'awaitMessages' of null Updated Code: message.author.send("What is your name?") const filter = m => m.author.id === message.author.id message.author.dmChannel.awaitMessages(filter) .then((collected) => console.log(collected.first().content)) 回答1: You're not using awaitMessages() properly, you need to pass a filter

Heroku H10 works on localhost, won't work in deployment

笑着哭i 提交于 2021-02-09 09:01:58
问题 This error seems to be pretty popular here! The app works great locally, but when deploying to Heroku... crickets, Application crashes. This is server.js var app = express(); var PORT = process.env.PORT || process.env.REACT_APP_PORT; app.use(express.urlencoded({ extended: true })); app.use(express.json()); (process.env.NODE_ENV === "production") ? app.use(express.static("build")) : app.use(express.static("public")); //code goes here app.use(cors()); app.get('*', function (req, res) { res

Heroku H10 works on localhost, won't work in deployment

混江龙づ霸主 提交于 2021-02-09 08:58:09
问题 This error seems to be pretty popular here! The app works great locally, but when deploying to Heroku... crickets, Application crashes. This is server.js var app = express(); var PORT = process.env.PORT || process.env.REACT_APP_PORT; app.use(express.urlencoded({ extended: true })); app.use(express.json()); (process.env.NODE_ENV === "production") ? app.use(express.static("build")) : app.use(express.static("public")); //code goes here app.use(cors()); app.get('*', function (req, res) { res

Socket.io Server-side callback

拜拜、爱过 提交于 2021-02-09 08:08:21
问题 I've successfully made a callback with http://socket.io when the client calls the server socket.on('event', function(data, fn){ fn(null, true); } I was wondering if its possible to do the same but this time when the server calls the client? 回答1: The emit and on works exactly same on both sides: client and server. So if you emit to client-socket on server side, then if on client-side you are subscribed to that event, it will trigger it. And vice versa. What you are confused by is that server

Socket.io Server-side callback

只愿长相守 提交于 2021-02-09 08:03:21
问题 I've successfully made a callback with http://socket.io when the client calls the server socket.on('event', function(data, fn){ fn(null, true); } I was wondering if its possible to do the same but this time when the server calls the client? 回答1: The emit and on works exactly same on both sides: client and server. So if you emit to client-socket on server side, then if on client-side you are subscribed to that event, it will trigger it. And vice versa. What you are confused by is that server

Sequelize.define() is not a function?

不羁的心 提交于 2021-02-09 07:26:54
问题 Trying to play around with Sequelize in a node.js webserver. I have initialised the Sequelize connection pool in index.js like so index.js const config = require('./config/config'); const app = require('./config/express'); const Sequelize = require('sequelize'); const sequelize = new Sequelize(config.mysql.database, config.mysql.user, config.mysql.pass, { host: config.mysql.host, dialect: 'mysql', operatorsAliases: false, pool: { max: 5, min: 0, acquire: 30000, idle: 10000 }, }); sequelize

Nodejs mongodb dynamic collection name

浪尽此生 提交于 2021-02-09 07:13:12
问题 I have several mongodb collections, Fruits: [ {}, {}, {} ...], Bread: [{}, {}, {} ...] I want to use dynamic collection name in my server like // :collection will be "Fruits" or "Bread" app.get('/fetch/:collection', function (req, res){ [req.params.collection].find({}, function(err, docs){ res.json(docs) }) }) But as you know, [req.params.collection].find()... isn't working. How can I use dynamic collection name? 回答1: This is not working as the collection name you are passing is just a