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
app.use(bodyParser.urlencoded({extended : true }))
app.get( '/' , (req, res) => {
res.send(formTemplet({}))
})
app.post( '/' , (req, res) => {
const {rawInput} = req.body
const buffer = Buffer.from(rawInput)
var uint8 = new Uint8Array(buffer)
const utf8str = arrayBufferToString(uint8)
console.log(`Original string : ${rawInput}\n`)
console.log(`Array Buffer : ${uint8}\n`)
console.log(`Decoded utf8 string : ${utf8str}\n`)
})
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=
<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
app.use(bodyParser.urlencoded({extended : true }))
app.get( '/' , (req, res) => {
res.send(formTemplet({}))
})
app.post( '/' , (req, res) => {
const {rawInput} = req.body
const buffer = Buffer.from(rawInput)
var uint8 = new Uint8Array(buffer)
const base64str = arrayBufferToString(uint8, 'base64' )
console.log(`Original string : ${rawInput}\n`)
console.log(`Array Buffer : ${uint8}\n`)
console.log(`Decoded utf8 string : ${base64str}\n`)
})
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=
<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.
Share your thoughts in the comments
Please Login to comment...