Categories
JavaScript Answers

How to get the path to the current script with Node.js?

Sometimes, we want to get the path to the current script with Node.js.

In this article, we’ll look at how to get the path to the current script with Node.js.

How to get the path to the current script with Node.js?

To get the path to the current script with Node.js, we can use __dirname .

__dirname returns a string with the path of the current script.

For ES nmodules, we can use import.meta.url instead.

Conclusion

To get the path to the current script with Node.js, we can use __dirname .

Categories
JavaScript Answers

How to use Node.js as a simple web server?

Sometimes, we want to use Node.js as a simple web server.

In this article, we’ll look at how to use Node.js as a simple web server.

How to use Node.js as a simple web server?

To use Node.js as a simple web server, we can use the connect and serve-static packages.

To install them, we run

npm install connect serve-static

Then we write the app.js script by writing

const connect = require('connect');
const serveStatic = require('serve-static');

connect()
  .use(serveStatic(__dirname))
  .listen(8080, () => console.log('Server running on 8080...'));

to call connect and use with the static file object returned by serveStatic called with the folder of the current project, which is __dirname.

And then we call listen with the port to listen to requests to and a callback that’s run when the server is running.

Then we run it with node app.js.

Conclusion

To use Node.js as a simple web server, we can use the connect and serve-static packages.

Categories
JavaScript Answers

How to get a list of the names of all files present in a directory in Node.js?

Sometimes, we want to get a list of the names of all files present in a directory in Node.js.

In this article, we’ll look at how to get a list of the names of all files present in a directory in Node.js.

How to get a list of the names of all files present in a directory in Node.js?

To get a list of the names of all files present in a directory in Node.js, we can call the readdir method.

For instance, we write

const testFolder = './folder/path';
const fs = require('fs');

fs.readdir(testFolder, (err, files) => {
  files.forEach(file => {
    console.log(file);
  });
});

to call fs.readdir with the testFolder folder path string and a callback that has all the files in an array.

In the callback, we call files.forEach to loop through each file entry.

Conclusion

To get a list of the names of all files present in a directory in Node.js, we can call the readdir method.

Categories
JavaScript Answers

How to get GET (query string) variables in Express.js on Node.js?

Sometimes, we want to get GET (query string) variables in Express.js on Node.js.

In this article, we’ll look at how to get GET (query string) variables in Express.js on Node.js.

How to get GET (query string) variables in Express.js on Node.js?

To get GET (query string) variables in Express.js on Node.js, we can get the from the req.query property.

For instance, we write

const express = require('express');
const app = express();

app.get('/', (req, res) => {
  res.send('id', req.query.id);
});

app.listen(3000);

to get the id query string parameter value with req.query.id.

Conclusion

To get GET (query string) variables in Express.js on Node.js, we can get the from the req.query property.

Categories
JavaScript Answers

How to check synchronously if a file or directory exists in Node.js?

Sometimes, we want to check synchronously if a file or directory exists in Node.js.

In this article, we’ll look at how to check synchronously if a file or directory exists in Node.js.

How to check synchronously if a file or directory exists in Node.js?

To check synchronously if a file or directory exists in Node.js, we can use the existsSync method.

For instance, we write

const fs = require("fs"); 
if (fs.existsSync(path)) {
  // ...
}

to call fs.existsSync with a path string to check if the path points to a valid file or folder.

It returns true if it does and false otherwise.

Conclusion

To check synchronously if a file or directory exists in Node.js, we can use the existsSync method.