Categories
JavaScript Answers

How to include functions from my other files with Node.js?

Sometimes, we want to include functions from my other files with Node.js

In this article, we’ll look at how to include functions from my other files with Node.js.

How to include functions from my other files with Node.js?

To include functions from my other files with Node.js, we can create our own module with module.exports.

For instance, we write

tools.js

module.exports = {
  foo() {
    // ...
  },
  bar() {
    // ...
  }
}

to assign module.exports to an object with the foo and bar functions in tools.js

Then we write

app.js

const tools = require('./tools');
tools.foo()
tools.bar()

to include the module in app.js by calling require with the path to tools.js and assign the module object to tools.

And then we can call the foo and bar functions from the module that we required.

Conclusion

To include functions from my other files with Node.js, we can create our own module with module.exports.

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.