Categories
JavaScript Answers

How to do base64 encoding in Node.js?

Sometimes, we want to do base64 encoding in Node.js.

In this article, we’ll look at how to do base64 encoding in Node.js.

How to do base64 encoding in Node.js?

To do base64 encoding in Node.js, we can use the Buffer.from method.

For instance, we write

const b = Buffer.from("Hello World").toString('base64')

to call Buffer.from with the string we want to encode.

Then we convert the returned buffer object to a base64 string by calling toString with 'base64'.

Conclusion

To do base64 encoding in Node.js, we can use the Buffer.from method.

Categories
JavaScript Answers

How to make a HTTP POST request in Node.js?

Sometimes, we want to make a HTTP POST request in Node.js.

In this article, we’ll look at how to make a HTTP POST request in Node.js.

How to make a HTTP POST request in Node.js?

To make a HTTP POST request in Node.js, we can use the axios package.

We install it by running

npm i axios

Then we use it by writing

const axios = require('axios');

const makeRequest = async () => {
  const data = {
    name: 'John Doe',
    job: 'Content Writer'
  };

  const {
    data: resData,
    status
  } = await axios.post('https://reqres.in/api/users', data)
  console.log(resData, status)
}

to call axios.post to make the request.

The data object is the JSON payload.

post returns a promise that resolves to an object with the data and status properties.

data has the response data and status has the response status code.

Conclusion

To make a HTTP POST request in Node.js, we can use the axios package.

Categories
JavaScript Answers

How to parse JSON using Node.js?

Sometimes, we want to parse JSON using Node.js.

In this article, we’ll look at how to parse JSON using Node.js.

How to parse JSON using Node.js?

To parse JSON using Node.js, we can use require.

For instance, we write

const config = require('./config.json');

to call require with the JSON file’s path and assign the returned JSON object to config.

require loads the JSON synchronously like any other module and the file is only read once when we call require and then cached thereafter.

Conclusion

To parse JSON using Node.js, we can use require.

Categories
JavaScript Answers

How to create a directory if it doesn’t exist using Node.js?

Sometimes, we want to create a directory if it doesn’t exist using Node.js.

In this article, we’ll look at how to create a directory if it doesn’t exist using Node.js.

How to create a directory if it doesn’t exist using Node.js?

To create a directory if it doesn’t exist using Node.js, we can use the existsSync method to check if a directory exists.

And then we can use the mkdirSync method to let us create the directory.

For instance, we write

const fs = require('fs');
const dir = './tmp';

if (!fs.existsSync(dir)) {
  fs.mkdirSync(dir);
}

to require the fs module.

Then we call fs.existsSync with dir to check if a folder with the path given in dir exists.

If it does, it returns false.

And we call mkdirSync with the dir path to create a directory with the path if the directory doesn’t exist.

Conclusion

To create a directory if it doesn’t exist using Node.js, we can use the existsSync method to check if a directory exists.

And then we can use the mkdirSync method to let us create the directory.

Categories
JavaScript Answers

How to access POST form fields with Node.js and Express?

Sometimes, we want to access POST form fields with Node.js and Express.

In this article, we’ll look at how to access POST form fields with Node.js and Express.

How to access POST form fields with Node.js and Express?

To access POST form fields with Node.js and Express, we can use the body-parser package.

To install it, we run

npm install --save body-parser

Then we write

const bodyParser = require('body-parser')
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
  extended: true
}));

to require the body-parser package.

And then we call app.use with bodyParser.json() to let us parse JSON payload from POST requests in our routes.

And we call bodyParser.urlencoded to let us parse URL encoded POST request payloads in our routes.

Conclusion

To access POST form fields with Node.js and Express, we can use the body-parser package.