Node.js NPM arraybuffer-to-string Module
Last Updated :
11 Jan, 2022
NPM(Node Package Manager) is a package manager of Node.js packages. There is an NPM package called arraybuffer-to-string used to decode array buffers in actual strings. The package not only converts the buffer to 'utf8' string but also it converts the buffer to many forms like base64 encoded string, a hex-encoded string that we use in many contexts .
Command to install:
npm install arraybuffer-to-string
Syntax to import the package in local file
const arrayBufferToString = require('arraybuffer-to-string')
Syntax to convert arraybuffer to string
arrayBufferToString(buffer, encodingType)
Parameters: It takes two parameter 'buffer' and 'encodingType' to which we want to convert the arraybuffer as shown below:
- buffer: It is the array buffer that we want to convert to the actual text. There are many methods in node.js that directly not returns the actual content of the string but return the buffer form of the response. In those contexts we use this package to convert the buffer to a string.
- encodingType: It is the optional parameter, default value is 'utf8'. it signifies the type of the string to which we want to convert the buffer. Available encoding are utf8, binary, base64, hex, ascii, latin1, ucs2, utf16 and many others.
Example 1: This example illustrates how to use 'arraybuffer-to string' to convert from array buffer to utf8 string.
Filename - index.js: This file contains logic to convert arraybuffer to utf8 string.
javascript
const express = require('express')
const bodyParser = require('body-parser')
const arrayBufferToString =require('arraybuffer-to-string')
const formTemplet = require('./form')
const app = express()
const port = process.env.PORT || 3000
// The body-parser middleware to parse form data
app.use(bodyParser.urlencoded({extended : true}))
// Get route to display HTML form
app.get('/', (req, res) => {
res.send(formTemplet({}))
})
// Post route to handle form submission logic and
app.post('/', (req, res) => {
const {rawInput} = req.body
// Creating buffer of string
const buffer = Buffer.from(rawInput)
// Buffer to array buffer
var uint8 = new Uint8Array(buffer)
// Converting from array buffer to actual utf8 string
const utf8str = arrayBufferToString(uint8)
// Printing on console
console.log(`Original string : ${rawInput}\n`)
console.log(`Array Buffer : ${uint8}\n`)
console.log(`Decoded utf8 string : ${utf8str}\n`)
})
// Server setup
app.listen(port, () => {
console.log(`Server start on port ${port}`)
})
filename-form.js: This file contain logic to render the form.
javascript
module.exports = ({errors}) => {
return `
<!DOCTYPE html>
<html>
<head>
<link rel='stylesheet' href=
'https://cdnjs.cloudflare.com/ajax/libs/bulma/0.9.0/css/bulma.min.css'>
<style>
div.columns {
margin-top: 100px;
}
.button {
margin-top: 10px
}
</style>
</head>
<body>
<div class='container'>
<div class='columns is-centered'>
<div class='column is-5'>
<form action='/' method='POST'>
<div>
<div>
<label class='label'
id='raw-input'>Raw Input
</label>
</div>
<input class='input' type='text'
name='rawInput' placeholder=
'Raw Input' for='raw-input'>
</div>
<div>
<button class='button is-info'>
Submit
</button>
</div>
</form>
</div>
</div>
</div>
</body>
</html>
`
}
Output:
Submit input string too convert it from string to array-buffer to utf8 encoded string
Response printed in command prompt
Example 2: This example illustrates how to use 'arraybuffer-to string' to convert from array buffer to base64 encoded string.
Filename - index.js: This file contains logic to convert arraybuffer to base64 encoded string.
javascript
const express = require('express')
const bodyParser = require('body-parser')
const arrayBufferToString =require('arraybuffer-to-string')
const formTemplet = require('./form')
const app = express()
const port = process.env.PORT || 3000
// The body-parser middleware to parse form data
app.use(bodyParser.urlencoded({extended : true}))
// Get route to display HTML form
app.get('/', (req, res) => {
res.send(formTemplet({}))
})
// Post route to handle form submission logic and
app.post('/', (req, res) => {
const {rawInput} = req.body
// Creating buffer of string
const buffer = Buffer.from(rawInput)
// Buffer to array buffer
var uint8 = new Uint8Array(buffer)
// Converting from array buffer to actual
// base64 encoded string
const base64str = arrayBufferToString(uint8, 'base64')
// Printing on console
console.log(`Original string : ${rawInput}\n`)
console.log(`Array Buffer : ${uint8}\n`)
console.log(`Decoded utf8 string : ${base64str}\n`)
})
// Server setup
app.listen(port, () => {
console.log(`Server start on port ${port}`)
})
filename-form.js : This file contain logic to render the form.
javascript
module.exports = ({errors}) => {
return `
<!DOCTYPE html>
<html>
<head>
<link rel='stylesheet' href=
'https://cdnjs.cloudflare.com/ajax/libs/bulma/0.9.0/css/bulma.min.css'>
<style>
div.columns {
margin-top: 100px;
}
.button {
margin-top: 10px
}
</style>
</head>
<body>
<div class='container'>
<div class='columns is-centered'>
<div class='column is-5'>
<form action='/' method='POST'>
<div>
<div>
<label class='label' id=
'raw-input'>Raw Input
</label>
</div>
<input class='input' type='text'
name='rawInput' placeholder=
'Raw Input' for='raw-input'>
</div>
<div>
<button class='button is-info'>
Submit
</button>
</div>
</form>
</div>
</div>
</div>
</body>
</html>
`
}
Output:
Submit input string too convert it from string to array-buffer to base64 encoded string
Response printed in command prompt
Note: We have used some Bulma classes in form.js file to design the content.
Similar Reads
JavaScript Tutorial JavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. It's an interpreted language that executes code line by line, providing more flexibility.JavaScript on Client Side: On the client side, Jav
11 min read
Web Development Web development is the process of creating, building, and maintaining websites and web applications. It involves everything from web design to programming and database management. Web development is generally divided into three core areas: Frontend Development, Backend Development, and Full Stack De
5 min read
React Interview Questions and Answers React is an efficient, flexible, and open-source JavaScript library that allows developers to create simple, fast, and scalable web applications. Jordan Walke, a software engineer who was working for Facebook, created React. Developers with a JavaScript background can easily develop web applications
15+ min read
JavaScript Interview Questions and Answers JavaScript (JS) is the most popular lightweight, scripting, and interpreted programming language. JavaScript is well-known as a scripting language for web pages, mobile apps, web servers, and many other platforms. Both front-end and back-end developers need to have a strong command of JavaScript, as
15+ min read
React Tutorial React is a JavaScript Library known for front-end development (or user interface). It is popular due to its component-based architecture, Single Page Applications (SPAs), and Virtual DOM for building web applications that are fast, efficient, and scalable.Applications are built using reusable compon
8 min read
Domain Name System (DNS) DNS is a hierarchical and distributed naming system that translates domain names into IP addresses. When you type a domain name like www.geeksforgeeks.org into your browser, DNS ensures that the request reaches the correct server by resolving the domain to its corresponding IP address.Without DNS, w
8 min read
REST API Introduction REST API stands for REpresentational State Transfer API. It is a type of API (Application Programming Interface) that allows communication between different systems over the internet. REST APIs work by sending requests and receiving responses, typically in JSON format, between the client and server.
7 min read
NodeJS Interview Questions and Answers NodeJS is one of the most popular runtime environments, known for its efficiency, scalability, and ability to handle asynchronous operations. It is built on Chromeâs V8 JavaScript engine for executing JavaScript code outside of a browser. It is extensively used by top companies such as LinkedIn, Net
15+ min read
HTML Interview Questions and Answers HTML (HyperText Markup Language) is the foundational language for creating web pages and web applications. Whether you're a fresher or an experienced professional, preparing for an HTML interview requires a solid understanding of both basic and advanced concepts. Below is a curated list of 50+ HTML
14 min read
What is an API (Application Programming Interface) In the tech world, APIs (Application Programming Interfaces) are crucial. If you're interested in becoming a web developer or want to understand how websites work, you'll need to familiarize yourself with APIs. Let's break down the concept of an API in simple terms.What is an API?An API is a set of
10 min read