typescript - 如何在打字稿快递服务器中将字符串解码/编码为base64
问题描述
我有一个用打字稿写的快递服务器。
作为atob()
或btoa()
在浏览器上工作,在 Nodejs 上。
我们一般使用
Buffer.from("some-string").toString('base64')
将字符串编码为base64。
但是,当我在 TypeScript 中编写代码时,这似乎不起作用。我需要一些帮助。
解决方案
在节点打字稿中:
const b64 = "SGVsbG8sIFdvcmxkIQ==";
const str = 'Hello, World!'
const decode = (str: string):string => Buffer.from(str, 'base64').toString('binary');
const encode = (str: string):string => Buffer.from(str, 'binary').toString('base64');
test('base64 decode', () => {
expect(decode(b64)).toEqual(str)
});
test('base64 decode', () => {
expect(encode(str)).toEqual(b64)
});
test('base64 encode/decode', () => {
expect(decode(encode(str))).toEqual(str)
});
推荐阅读
- python - 删除 Tkinter 条目中的最后一个空白行
- flutter - Flutter FFMPEG:设置配置文件基线时出错
- linux - 如何使用 tail -f 命令将 grep 的输出传递给 sed
- ionic5 - 我可以在 IONIC 5 项目中使用插件“angular2-swing”吗?
- java - 从骆驼请求中读取正文
- angular - 在没有 DenyAddAndCustomizePages $false 的情况下在 SharePoint Online 页面中集成 Angular 代码
- javascript - ajax 在 for loop django formset 中不起作用
- javascript - 如何在角度提交按钮后显示值?
- javascript - 离开 iframe 时 Wheel 事件未监听
- javascript - 查看 Google Analytics 4 界面时,setTimeout、setInterval 在 Chrome 扩展中不起作用