Categories
JavaScript Answers

How to stop Node.js program from command line?

Sometimes, we want to stop Node.js program from command line.

In this article, we’ll look at how to stop Node.js program from command line.

How to stop Node.js program from command line?

To stop Node.js program from command line, we can use a few key combos or commands.

We can use ctrl + z to suspend the program.

We can use ctrl + c to stop the program.

Also, we can use

ps aux | grep node

to find the process ID of the node program.

Then we can use

kill 9 PROCESS_ID

to run kill with the node‘s process ID to kill node.

We can also use

killall node

to kill all instances of the node program.

Conclusion

To stop Node.js program from command line, we can use a few key combos or commands.

We can use ctrl + z to suspend the program.

We can use ctrl + c to stop the program.

Categories
JavaScript Answers

How to get data from fs.readFile?

Sometimes, we want to get data from fs.readFile.

In this article, we’ll look at how to get data from fs.readFile.

How to get data from fs.readFile?

To get data from fs.readFile, we can get it from the 2nd parameter of the callback.

For instance, we write

const fs = require('fs');

fs.readFile('./index.html', (err, data) => {
  if (err) {
    throw err;
  }

  console.log(data)
});

to call fs.readFile with the path of the file to read and a callback that has the read file.

We get the read file content from the data parameter.

Conclusion

To get data from fs.readFile, we can get it from the 2nd parameter of the callback.

Categories
JavaScript Answers

How to fix Express.js not getting static files?

Sometimes, we want to fix Express.js not getting static files.

In this article, we’ll look at how to fix Express.js not getting static files.

How to fix Express.js not getting static files?

To fix Express.js not getting static files, we call express.staric with the path to the static files.

For instance, we write

app.use("/styles", express.static(__dirname + '/styles'));

to call app.use with the path to the static files and the middleware returned by expres.static called with the path to the /styles folder in the directory.

__dirname is the path of the Express project folder.

As a result, we should be able to access the files in the styles folder with the /styles path.

Conclusion

To fix Express.js not getting static files, we call express.staric with the path to the static files.

Categories
JavaScript Answers

How to get all directories within directory with Node.js?

Sometimes, we want to get all directories within directory with Node.js.

In this article, we’ll look at how to get all directories within directory with Node.js.

How to get all directories within directory with Node.js?

To get all directories within directory with Node.js, we can use the readdir method.

For instance, we write

const {
  promises: {
    readdir
  }
} = require('fs')

const getDirectories = async source => {
  const dirs = await readdir(source, {
    withFileTypes: true
  })
  return dirs
    .filter(dirent => dirent.isDirectory())
    .map(dirent => dirent.name)
}

to create the getDirections function that takes the source path string.

In it, we call readdir with source and an object that has withFileTypes set to true to return the file types with the items.

Then we call isDirectory to return an array of directories in the source folder and call map to return the names of each folder.

Conclusion

To get all directories within directory with Node.js, we can use the readdir method.

Categories
JavaScript Answers

How to get remote client address with Node.js and Express.js?

Sometimes, we want to get remote client address with Node.js and Express.js.

In this article, we’ll look at how to get remote client address with Node.js and Express.js.

How to get remote client address with Node.js and Express.js?

To get remote client address with Node.js and Express.js, we can use the req.headers or req.socket.remoteAddress properties.

For instance, we write

const ip = req.headers['x-forwarded-for'] || req.socket.remoteAddress

to get the client IP address from the x-forwarded-for request header with req.headers['x-forwarded-for'].

If that’s not set, then we use req.socket.remoteAddress to get the client IP address.

Conclusion

To get remote client address with Node.js and Express.js, we can use the req.headers or req.socket.remoteAddress properties.