Categories
JavaScript Answers

How to create a directory if it doesn’t exist using Node.js?

Sometimes, we want to create a directory if it doesn’t exist using Node.js.

In this article, we’ll look at how to create a directory if it doesn’t exist using Node.js.

How to create a directory if it doesn’t exist using Node.js?

To create a directory if it doesn’t exist using Node.js, we can use the existsSync method to check if a directory exists.

And then we can use the mkdirSync method to let us create the directory.

For instance, we write

const fs = require('fs');
const dir = './tmp';

if (!fs.existsSync(dir)) {
  fs.mkdirSync(dir);
}

to require the fs module.

Then we call fs.existsSync with dir to check if a folder with the path given in dir exists.

If it does, it returns false.

And we call mkdirSync with the dir path to create a directory with the path if the directory doesn’t exist.

Conclusion

To create a directory if it doesn’t exist using Node.js, we can use the existsSync method to check if a directory exists.

And then we can use the mkdirSync method to let us create the directory.

Categories
JavaScript Answers

How to access POST form fields with Node.js and Express?

Sometimes, we want to access POST form fields with Node.js and Express.

In this article, we’ll look at how to access POST form fields with Node.js and Express.

How to access POST form fields with Node.js and Express?

To access POST form fields with Node.js and Express, we can use the body-parser package.

To install it, we run

npm install --save body-parser

Then we write

const bodyParser = require('body-parser')
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
  extended: true
}));

to require the body-parser package.

And then we call app.use with bodyParser.json() to let us parse JSON payload from POST requests in our routes.

And we call bodyParser.urlencoded to let us parse URL encoded POST request payloads in our routes.

Conclusion

To access POST form fields with Node.js and Express, we can use the body-parser package.

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.