Node JS Interview Questions (Basic to Advanced) Part 4

Tasadduq Ali
2 min readMay 4, 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

  1. 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);
});

2. What are async/await in Node.js?

Async/await is a newer syntax introduced in Node.js 8 for handling asynchronous operations. It allows you to write asynchronous code that looks more like synchronous code. Here's an example:

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

async function main() {
const data = await fetchData();
console.log(data);
}

main().catch((error) => {
console.error(error);
});

3. How do you use middleware in Node.js?

Middleware is a function that sits between a request and a response in a Node.js application. It can be used to modify requests and responses, or to perform additional processing. Here's an example:

javascriptCopy code
function logger(req, res, next) {
console.log(`${req.method} ${req.url}`);
next();
}

app.use(logger);

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

Express.js is a popular Node.js web framework used for building server-side applications. Here's an example of creating a simple Express.js server:

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

const app = express();

app.get('/', (req, res) => {
res.send('Hello, World!');
});

app.listen(3000, () => {
console.log('Server running on <http://localhost:3000/>');
});

5. How do you use Socket.io in Node.js?

Socket.io is a library that allows real-time, bi-directional communication between clients and servers in a Node.js application. Here's an example of creating a simple Socket.io server:

javascriptCopy code
const http = require('http');
const io = require('socket.io');

const server = http.createServer((req, res) => {
res.writeHead(200);
res.end('Hello, World!');
});

const socket = io(server);

socket.on('connection', (client) => {
console.log('Client connected');
});

server.listen(3000, () => {
console.log('Server running on <http://localhost:3000/>');
});

if you have any questions or suggestions just do let me know on my Instagram or at codeculturepro@gmail.com

--

--

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 😉

Responses (2)