9951 explained code solutions for 126 technologies


expressjsWhat are some common Express.js interview questions?


  1. What is Express.js? Express.js is a web application framework for Node.js that provides a robust set of features for web and mobile applications. It is used to create server-side applications and APIs.

  2. What are the main features of Express.js? The main features of Express.js include routing, middleware, template engines, and database integration. It also provides support for various databases, such as MySQL, MongoDB, and Redis.

  3. What is a middleware in Express.js? Middleware in Express.js is a function that is invoked when a request is made to the server. It is used to perform operations on the request and response objects before they are sent to the next middleware or route handler.

Example

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

// A middleware function
function logger(req, res, next) {
  console.log(`${req.method} ${req.url}`);
  next();
}

app.use(logger);

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

app.listen(3000, () => {
  console.log('Server started on port 3000');
});

Output example

GET /
Server started on port 3000
  1. What is a template engine in Express.js? A template engine in Express.js is a library that allows you to generate HTML dynamically from data. It is used to create webpages from data stored in the backend.

Example

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

// Set view engine
app.set('view engine', 'ejs');

app.get('/', (req, res) => {
  // Render the index.ejs template
  res.render('index', {
    title: 'Express App',
    message: 'Hello World!'
  });
});

app.listen(3000, () => {
  console.log('Server started on port 3000');
});
  1. What is routing in Express.js? Routing in Express.js is the process of mapping URLs to callback functions (also known as controller functions). It is used to define the behavior of an application when a client makes a request to a particular URL.

Example

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

// Define routes
app.get('/', (req, res) => {
  res.send('Home page');
});

app.get('/about', (req, res) => {
  res.send('About page');
});

app.listen(3000, () => {
  console.log('Server started on port 3000');
});
  1. What is database integration in Express.js? Database integration in Express.js is the process of connecting an application to a database. It is used to store and retrieve data from a database.

Example

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

// Create connection
const db = mysql.createConnection({
  host: 'localhost',
  user: 'root',
  password: 'password',
  database: 'mydatabase'
});

// Connect
db.connect((err) => {
  if (err) {
    throw err;
  }
  console.log('MySQL connected...');
});

app.get('/', (req, res) => {
  let sql = 'SELECT * FROM posts';
  let query = db.query(sql, (err, results) => {
    if (err) throw err;
    console.log(results);
    res.send('Posts fetched...');
  });
});

app.listen(3000, () => {
  console.log('Server started on port 3000');
});

Output example

MySQL connected...
[ { id: 1, title: 'Post One', body: 'This is post one' },
  { id: 2, title: 'Post Two', body: 'This is post two' } ]
Posts fetched...

Helpful links

Edit this code on GitHub