Create a custom route variable in Node.js with Express framework

Is there a way to achieve this particular task using express.js?

In my express.js application, I have set up a route like so:

app.get('/hello', (req, res) => {
  // Code goes here
});

This setup is functional, but I am curious if it is possible to introduce a variable instead:

app.get('/' + myVariable, (req, res) => {

  // Here is where I would like to work with the value of myVariable:

  if (res === 'something') {
     // Carry out specific action
  } else if (res === 'somethingelse') {
    // Perform another action
  }

  // Alternatively, utilize a SWITCH CASE statement...

});

Is there a method to implement something similar to this in express.js?

Answer №1

If you want to achieve this, you can utilize Route parameters.

app.get('/:myVariable', (req, res) => {
  const myVar = req.params.myVariable;

  if (myVar === 'something') {
     // do a specific action
  } else if (myVar === 'somethingelse') {
    // perform another action
  }
});

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

activating javascript function in rails when the page reloads

I have a rails4 app and I'm looking to implement some specific JavaScript events when the page loads, but only if the user is coming from a certain page. On the post#index page, all comment replies are initially hidden. When a user clicks on a specif ...

Utilizing Regular Input with Material-UI's Autocomplete Feature

Is there a way to replace TextField with a regular input in an Autocomplete? ...

The installation of npm failed due to a command error with legacy peer dependencies

Attempting to address a problem related to node-sass peer dependencies, I decided to execute the following command: npm install --legacy-peer-deps However, upon running this command, I encountered the following stack trace which is causing confusion: npm ...

MySQL X Dev API update function malfunctioning

Struggling to find a suitable template for the update method in the xdevapi library, I am reaching out for assistance in making this work. acts_table .update() .set('account_status', 'new account status') .where('custom ...

Ways to incorporate user information, such as its database ID, prior to initiating and capturing an order using PayPal Version 2 in a

I am currently in the process of developing a Flutter e-commerce application backend using Node.js. I have integrated PayPal through version 2 as the previous version, v1, has been deprecated. However, I am facing a challenge regarding how to include a u ...

Is there a way to change the visibility of a form element within a directive with the help of

Consider a scenario where you have a basic form as shown below: <form ng-submit="methods.submit(formData)" ng-controller="diamondController" name="diamond" novalidate> <help-block field="diamond.firstName"></help-block> <input ...

How to Modify the Data in a Dynamic Object within the Vuex Store in a Nuxt Application

Within my Vue/Nuxt project, I have implemented a form where users can add and update dynamic fields for calculating price offers. Upon loading the form, one field is created using the beforeMount lifecycle, with the option for users to add more fields as n ...

Create an array of arrays within a loop using TypeScript

My application contains an object with dates and corresponding time arrays. The console log output is displayed below: 32: { 1514160000: Array [ 1200, 1500 ], 1514764800: Array [ 1200, 1500 ], 1515369600: Array [ 1200, 1500 ], 1515974400: Array [ 700, 12 ...

System access denied to create directory node_modules on MacOS Monterey

I can't figure out how to resolve this ongoing issue... I've attempted uninstalling node entirely and then reinstalling, clearing the npm cache, reinstalling the packages (Angular CLI), using sudo chown -R franfonse ../Programming , but this prob ...

An object in typescript has the potential to be undefined

Just starting out with Typescript and hitting a snag. Can't seem to resolve this error and struggling to find the right solution useAudio.tsx import { useEffect, useRef } from 'react'; type Options = { volume: number; playbackRate: num ...

Is there a way to make divs expand on top of existing content when hovering over them, in order to avoid needing to scroll through overflow content? I am currently working with

I am working with 9 boxes contained within divs, each box includes data that exceeds the size of the box itself (represented by Some Text repeated for demonstration purposes). I am seeking a solution where hovering over any box will cause it to enlarge and ...

Encountering errors while trying to npm install on Ubuntu system

When attempting to install a package using npm install -g PACKAGE_NAME, I encountered the following error message. npm http GET https://registry.npmjs.org/ionic npm http 304 https://registry.npmjs.org/ionic npm ERR! Error: EACCES, mkdir '/usr/local/l ...

I'm puzzled as to why I am unable to invoke a class method within this callback function. The error message indicates a TypeError: 'this' is undefined

Currently, I am troubleshooting a challenge in my Angular application that involve TypeScript. The issue lies within a method in a TypeScript class: findArtistBidsAppliedByCurrentWall(bid):Observable<Bid[]> { console.log("findArtistBidsApplied ...

The Node.js application encounters a blank response when making a GET request to the API

As a newcomer to node.js, I'm attempting to describe the issue at hand as clearly as possible. If clarification is needed, please let me know. In my current node.js project, I am faced with a challenge where I need to take a code received from the re ...

"Encountering an issue with Next.js where the Redux

Hey there, I'm working on a simple project using Nextjs. I need to access the state of my Redux store but I'm encountering an error when trying to use store.getState, it's throwing an error saying getState is undefined. Additionally, I have ...

Is there a way to extract a token from the URL in a Nextjs/React application?

I am currently developing a project that relies heavily on API integration. My front-end interface is built using React and Next.js, while the back-end API is developed with Laravel. Within my front-end page, I have implemented a token in the URL to help ...

There seems to be a lack of response from the Foursquare API in Node.js

Seeking guidance on retrieving a photo from a Foursquare venue using Node.js platform. Despite having the correct id and secret, the code is returning an unexpected result. My goal is to obtain the prefix and suffix of the image to properly display it as o ...

What are the best ways to implement advanced data filtering in JavaScript?

Is there a way to improve the filtering of data in the ngx-datatable? Currently, I have a filter that only works for the "Name" column. How can I adjust it to filter data from other columns like "id" or "phone" as well? filt ...

Addon for Firefox: Image Upload

I am looking for a way to streamline the process of uploading an image to a website through a Firefox Addon. While I know it is possible to use createElement('canvas'), convert Image data to base64, and XHR POST the data, I would prefer to lever ...

Preserve the original position of the inner <div> when animating the container <div> using JQuery

I am currently working on a small website and encountering some challenges with jQuery animation. My issue involves placing a single character text inside a circle and wanting it to grow when the user hovers over it, while keeping the inner text in its ori ...