WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Node.js · Intermediate · question 23 of 100

How can you create a RESTful API using Express.js in Node.js?

📕 Buy this interview preparation book: 100 Node.js questions & answers — PDF + EPUB for $5

Creating a RESTful API using Express.js in Node.js involves the following steps:

1. **Install Node.js and Express.js**: First, you need to have Node.js installed on your computer. If you haven’t installed it yet, download and install it from the official website: https://nodejs.org. Next, create a new folder for your project and run the following command in the terminal to initialize a new Node.js application:

npm init

Then, install the Express.js package using the following command:

npm install express

2. **Set up your Express.js app**: Create a new file in your project folder, for example, ‘app.js‘, and import the ‘express‘ package. Here’s a basic setup for your Express.js app:

// Import the express package
const express = require('express');

// Initialize the express app
const app = express();

// Set up the app to parse JSON data
app.use(express.json());

// Start the server on a specific port
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
  console.log(`Server is running on port ${PORT}`);
});

3. **Define your routes**: Define routes (endpoints) for your RESTful API to handle different HTTP methods such as GET, POST, PUT, and DELETE. Here’s an example of how to create a simple in-memory representation of data and define RESTful routes for CRUD operations:

// In-memory data storage
const data = [
  { id: 1, name: 'Item 1' },
  { id: 2, name: 'Item 2' },
];

// GET route to return all data
app.get('/api/data', (req, res) => {
  res.send(data);
});

// GET route to return a specific data item by id
app.get('/api/data/:id', (req, res) => {
  const item = data.find((item) => item.id === parseInt(req.params.id));
  if (!item)
    return res.status(404).send('The item with the given ID was not found.');
  res.send(item);
});

// POST route to create a new data item
app.post('/api/data', (req, res) => {
  const newItem = {
    id: data.length + 1,
    name: req.body.name,
  };
  data.push(newItem);
  res.send(newItem);
});

// PUT route to update an existing data item by id
app.put('/api/data/:id', (req, res) => {
  const item = data.find((item) => item.id === parseInt(req.params.id));
  if (!item)
    return res.status(404).send('The item with the given ID was not found.');

  item.name = req.body.name;
  res.send(item);
});

// DELETE route to delete a data item by id
app.delete('/api/data/:id', (req, res) => {
  const item = data.find((item) => item.id === parseInt(req.params.id));
  if (!item)
    return res.status(404).send('The item with the given ID was not found.');

  const index = data.indexOf(item);
  data.splice(index, 1);

  res.send(item);
});

Now your Express.js app is set up to handle basic CRUD operations for a simple in-memory data storage. The RESTful API routes you created can be tested using tools like Postman or CURL.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Node.js interview — then scores it.
📞 Practice Node.js — free 15 min
📕 Buy this interview preparation book: 100 Node.js questions & answers — PDF + EPUB for $5

All 100 Node.js questions · All topics