Node JS Interview Questions (Basic to Advanced)Part 3

Tasadduq Ali
2 min readMay 1, 2023

--

Today I am continuing Node JS Interview Question Series. This series will be based on 5 parts covering the important questions asked in interviews from Basic to Advanced

Before starting. If you want to be the part of CODE Culture Community. Just fill out the form to join the Free Code Culture Sessions on Sunday and get your questions answered about coding and career. I am form click me

Before starting. If you want to be the part of CODE Culture Community. Just fill out the form to join the Free Code Culture Sessions on Sunday and get your questions answered about coding and career. I am form click me

  1. How do you create a new directory in Node.js?
javascriptCopy code
const fs = require('fs');

fs.mkdir('my-directory', (err) => {
if (err) throw err;
console.log('Directory created');
});

2. How do you remove a directory in Node.js?

javascriptCopy code
const fs = require('fs');

fs.rmdir('my-directory', (err) => {
if (err) throw err;
console.log('Directory removed');
});

3. How do you create a child process in Node.js?

javascriptCopy code
const { spawn } = require('child_process');

const child = spawn('ls', ['-lh', '/usr']);

child.stdout.on('data', (data) => {
console.log(`stdout: ${data}`);
});

child.stderr.on('data', (data) => {
console.error(`stderr: ${data}`);
});

child.on('close', (code) => {
console.log(`child process exited with code ${code}`);
});

4. How do you use callbacks in Node.js?

Callbacks are a common pattern in Node.js for handling asynchronous operations. Here's an example:

javascriptCopy code
function fetchData(callback) {
setTimeout(() => {
const data = 'Data fetched';
callback(data);
}, 2000);
}

fetchData((data) => {
console.log(data);
});

5. How do you use promises in Node.js?

Promises are a way to handle asynchronous operations in a more structured and organized way. Here's an example:

javascriptCopy code
function fetchData() {
return new Promise((resolve, reject) => {
setTimeout(() => {
const data = 'Data fetched';
resolve(data);
}, 2000);
});
}

fetchData().then((data) => {
console.log(data);
}).catch((error) => {
console.error(error);
});

--

--

Tasadduq Ali
Tasadduq Ali

Written by Tasadduq Ali

I am MERN Stack developer working in UAE Govt to digitize their massive services. I will help you to become highly skilled Coder 😉

No responses yet