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.

Categories
JavaScript Answers

How to include functions from my other files with Node.js?

Sometimes, we want to include functions from my other files with Node.js

In this article, we’ll look at how to include functions from my other files with Node.js.

How to include functions from my other files with Node.js?

To include functions from my other files with Node.js, we can create our own module with module.exports.

For instance, we write

tools.js

module.exports = {
  foo() {
    // ...
  },
  bar() {
    // ...
  }
}

to assign module.exports to an object with the foo and bar functions in tools.js

Then we write

app.js

const tools = require('./tools');
tools.foo()
tools.bar()

to include the module in app.js by calling require with the path to tools.js and assign the module object to tools.

And then we can call the foo and bar functions from the module that we required.

Conclusion

To include functions from my other files with Node.js, we can create our own module with module.exports.