Categories
JavaScript Answers

How to get last modified file date in Node.js?

To get last modified file date in Node.js, we use the stats.mtime property.

For instance, we write

fs.stat("/dir/file.txt", (err, stats) => {
  const mtime = stats.mtime;
  console.log(mtime);
});

to call fs.stat to get the file metadata for the /dir/file.txt file.

And we use stats.mtime to get the last modified date.

Categories
JavaScript Answers

How to restart a Node.js server?

To restart a Node.js server, we use the pkill command.

For instance, we run

pkill -HUP node

to kill all Node.js processes with pkill.

Then we start node again.

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.