Categories
JavaScript Answers

How to restart Node upon changing a file?

Sometimes, we want to restart Node upon changing a file.

In this article, we’ll look at how to restart Node upon changing a file.

How to restart Node upon changing a file?

To restart Node upon changing a file, we can use the supervisor package.

We install it by running

npm install supervisor -g

Then we can watch for file changes in the Node project with entry point app.js by running

supervisor app.js

Conclusion

To restart Node upon changing a file, we can use the supervisor package.

Categories
JavaScript Answers

How to move files in Node.js?

Sometimes, we want to move files in Node.js.

In this article, we’ll look at how to move files in Node.js.

How to move files in Node.js?

To move files in Node.js, we can use the fs.rename method.

For instance, we write

const fs = require('fs')

const oldPath = 'old/path/file.txt'
const newPath = 'new/path/file.txt'

fs.rename(oldPath, newPath, (err) => {
  if (err) {
    throw err
  }
  console.log('successfully moved')
})

to call rename with the oldPath of the file, newPath of the file, and a callback that runs when the moving is done.

If there’s an error, err will be set.

Conclusion

To move files in Node.js, we can use the fs.rename method.

Categories
JavaScript Answers

How to read a text file into an array with each line an item in the array?

Sometimes, we want to read a text file into an array with each line an item in the array.

In this article, we’ll look at how to read a text file into an array with each line an item in the array.

How to read a text file into an array with each line an item in the array?

To read a text file into an array with each line an item in the array, we can use the readFileSync or readFile method.

For instance, we write

const fs = require('fs');

const array = fs.readFileSync('file.txt').toString().split("\n");
for (const a of array) {
  console.log(a);
}

to call fs.readFileSync to read 'file.txt' synchronously.

Then we call toString to return the read file as a string.

And then we split the string into an array with each line as an item with split.

Then we loop through each array entry with the for-of loop.

We call readFile to read the file asynchronously.

To use it, we write

const fs = require('fs');

fs.readFile('file.txt', (err, data) => {
  if (err) {
    throw err;
  }
  
  const array = data.toString().split("\n");
  for (const a of array) {
    console.log(a);
  }
});

to call readFile to read 'file.txt'.

The callback is run when the file is read.

And we convert the file to a string, split the lines, and loop through them the same way.

Conclusion

To read a text file into an array with each line an item in the array, we can use the readFileSync or readFile method.

Categories
JavaScript Answers

How to create a proxy with Express.js?

Sometimes, we want to create a proxy with Express.js.

In this article, we’ll look at how to create a proxy with Express.js.

How to create a proxy with Express.js?

To create a proxy with Express.js, we can use the express-http-proxy package.

To install it, we run

npm i express-http-proxy

Then we write

const url = require('url');
const proxy = require('express-http-proxy');

const apiProxy = proxy('example.com:3000/test', {
  proxyReqPathResolver: req => url.parse(req.baseUrl).path
});

to call proxy with the URL we want to proxy and an object that has the proxyReqPathResolver property set to a function to map the URL we’re proxy to a URL in our app.

url.parse parses the URL and we get the URL path with path.

Conclusion

To create a proxy with Express.js, we can use the express-http-proxy package.

Categories
JavaScript Answers

How to return the current timestamp with Moment.js?

Sometimes, we want to return the current timestamp with Moment.js.

In this article, we’ll look at how to return the current timestamp with Moment.js.

How to return the current timestamp with Moment.js?

To return the current timestamp with Moment.js, we can use the unix method.

For instance, we write

const currentDate = moment().unix();

to return the current UNIX timestamp.

Conclusion

To return the current timestamp with Moment.js, we can use the unix method.