Categories
JavaScript Answers

How to load font-awesome using SCSS (SASS) in Webpack using relative paths with JavaScript?

To load font-awesome using SCSS (SASS) in Webpack using relative paths with JavaScript, we set the $fa-font-path variable.

For instance, we write

$fa-font-path: "~font-awesome/fonts";
@import "~font-awesome/scss/font-awesome"

to set the $fa-=font-path variable before we use @import to import font awesome.

Categories
JavaScript Answers

How to construct Http Post URL with form params with the JavaScript Axios Http client?

To construct Http Post URL with form params with the JavaScript Axios Http client, we use the querystring module.

For instance, we write

const querystring = require("querystring");
//...
await axios.post(
  authServerUrl,
  querystring.stringify({
    username: "abcd",
    password: "1235!",
    client_id: "user-client",
  }),
  {
    headers: {
      "Content-Type": "application/x-www-form-urlencoded",
    },
  }
);

to call post with the authServerUrl and a query string we get by calling querystring.stringiy with an object with the query parameters.

And we set the request headers by setting headers to an object with the headers.

Categories
JavaScript Answers

How to run a Node.js script from within another Node.js script?

To run a Node.js script from within another Node.js script, we cal;l the fork method.

For instance, we write

require('child_process').fork('app.js'); 

to call fork to run app.js.

Then in app.js, we write

console.log('calling form parent process');

to add a console log.

Categories
JavaScript Answers

How to install a list of many global packages with Yarn and JavaScript?

To install a list of many global packages with Yarn and JavaScript, we run the yarn global add command.

To do this, we run

yarn global add nodejs

to install global packages for Node.js.

Categories
JavaScript Answers

How to make Node.js http ‘get’ request with query string parameters?

To make Node.js http ‘get’ request with query string parameters, we set the qs property.

For instance, we write

const request = require("request");
const propertiesObject = { field1: "test1", field2: "test2" };

request({ url, qs: propertiesObject }, (err, response, body) => {
  if (err) {
    console.log(err);
    return;
  }
  console.log(response.statusCode);
});

to call request to make a request to url and we set the quert string by setting qs to an object with the keys and values for the query string.

Then we get the response from the response parameter and the response body from the body parameter.