Organizing express queries independently of routes

I am currently working on creating an express route that allows me to input an equity name as a query parameter by using ?symbol= in the URL. Once the equity name is provided, I intend to include a new route after that.

const express = require("express")
const app = express()

app.get("/api/v1/equity/latest", (req, res) => {
    res.send(req.query)
})

app.listen (3000, () => {
    console.log("listening on port 3000")
})

When I send a GET request to the URL

localhost:3000/api/v1/equity?symbol=BBNI/latest/
and examine the received queries, it shows symbol = BBNI/latest/

I am wondering how I can effectively separate the symbol query from the subsequent /latest route?

Answer №1

When looking at this particular URL format, it may strike developers as strange:

/v1/equity?symbol=BBNI/latest/

At first glance, it appears that the symbol parameter is simply BBNI/latest/ instead of just BBNI

One way to separate these components is by utilizing the following code:

const [symbol, latest] = req.query.symbol.split('/', 2);

It may be wise to rethink and design a more coherent URL structure for better clarity.

Answer №2

I believe there may be a better approach.

Please consider this alternative solution.

const express = require("express")
const app = express()

app.get("/api/v1/equity/:symbol/latest", (req, res) => {
    res.send(req.params.symbol)
})

app.listen (3000, () => {
    console.log("listening on port 3000")
})

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

The recently added item in nestedSortable remains stationary and does not shift positions

const menu = $("ol.menu").nestedSortable({ handle: '.move', items: 'li', placeholder: 'placeholder', opacity: 0.7, toleranceElement: '> i', c ...

Avoid displaying identical items when rendering a page from JSON data

I am using ajax and json to render a page. The structure of my json is as follows: {"status":"ok","rewards":[{"id":201,"points":500},{"id":202,"points":500}]}. I want to load the data using ajax only once if 'points' have duplicates in any of the ...

execute a setTimeout function in ReactJs to render a component after a specified amount of time

Is there a way to render a component inside my App.Js after a certain amount of time using setTimeout? I've tried, but nothing seems to be happening... This is my code: function App() { return ( <Router> <GlobalStyle /> ...

Processing output in Node.js with Express and child_process

Is there a way for an express server to handle requests to a child_process? For instance, sending a command to cmd.exe and receiving the response to send back to the client. Thank you. UPDATE: This is how I attempted it: Firstly, spawning the child_pr ...

The "main" entry for ts-node is not valid when running ts-node-dev

Recently, I embarked on a TypeScript project using yarn where I executed the following commands: yarn init -y yarn add typescript -D yarn tsc --init yarn add ts-node-dev -D Subsequently, I crafted a script titled dev that triggers tsnd src/index.ts, howev ...

jQuery is not updating the div as expected

While typing out this question, I'm hoping to uncover a solution that has eluded me so far. However, just in case... About a year ago, I successfully implemented something similar on another website and went through the code meticulously. Surprisingl ...

Setting a default value in a multi-select dropdown using react-select

I have taken over management of my first React app, and I am facing a seemingly simple change that needs to be made. The modification involves an email signup page where users can select their interests from a multi-select dropdown menu. My task is to mak ...

Verify the values in the dropdown menu and then cross-reference them with the user's input

Is there a way to verify if the textfield, newTeamName, is already included in a list of teamnames that are stored within a select box? I seem to be encountering issues with my code - can you point out what might be the problem? Just to note, there are no ...

Guide to creating a ReactJS higher-order component using modern ES6 syntax

I´m attempting to create a ReactJS higher-order component using ES6 syntax. This is what I have so far: export const withContext = Component => class AppContextComponent extends React.Component { render() { return ( ...

Change the color of this element and input field background

Having trouble with setting the background color of an input field to red in this code: $(document).ready(function(){ $(".abc").focus(function(){ $(this).attr('background', 'red'); $("label").text('Insert tex ...

Tips for utilizing setState to display specific information fetched from an API call through the mapping method

Is there a way to utilize setState in order to render individual data retrieved from an API call? Despite my efforts, all I seem to get is another array of data. Here's the code snippet: const [likes, setLikes] = useState(0); useEffect( async () = ...

Using PHP to display a message box and redirecting to a different webpage

Currently, I am working on developing a login page using PHP. When the user enters an incorrect username/password combination, I display a JavaScript alert box to notify them. However, after clicking the "OK" button on the alert box, I want to redirect t ...

Populating a table with information via AJAX requests and multiple API calls

Currently, I am working with a table that contains numerous columns filled with data obtained in JSON format from various API sources using jQuery AJAX. Initially, I believed this to be an ideal solution due to its ease of handling concurrency with JavaScr ...

Ensuring the accurate usage of key-value pairs in a returned object through type-checking

After generating a type definition for possible response bodies, I am looking to create a function that returns objects shaped as { code, body }, which are validated against the typing provided. My current solution looks like this: type Codes<Bodies> ...

Having a problem with the glitch effect in Javascript - the text is oversized. Any tips on how to resize

I found some interesting code on the internet that creates a glitch text effect. However, when I implement it, the text appears too large for my webpage. I'm struggling to adjust the size. Here is the current display of the text: too big This is how ...

Setting URL parameters dynamically to the action attribute of a form using JavaScript code

I'm having trouble posting Form data to my Server. I am dynamically setting the element and its URL parameters for the action attribute, but it seems like it's not recognizing those attributes. Can you help me figure out what I'm doing wrong ...

Utilizing adapter headers in contexts other than ActiveModelAdapter

I have successfully implemented my authorization system with Ember Data. All my ember-data calls are secure and signed correctly using adapter.ajax() instead of $.ajax. However, I am facing a situation where I need to utilize a third-party upload library t ...

Display HTML code within a data attribute

I have a function that modifies an element from OpenLayers. In the official documentation, it mentions that the property label accepts either HTML or a string. methods: { onUpdatePosition (coordinate) { this.deviceCoordinate = coordinat ...

What is the method to assign multiple values to ng-disabled in AngularJS?

Is there a way to assign multiple values for ng-disabled in AngularJS? The issue I'm facing is demonstrated in the following JS Fiddle: http://jsfiddle.net/FJf4v/10/ <div ng-app> <div ng-controller="myCnt"> <h3>A ->> ...

Having trouble with moving the svg:svg element?

I'm struggling to move an svg element that is nested within another svg. I am trying to directly manipulate the x and y values, but I keep encountering a "read-only" error. I attempted to use transform instead, but it doesn't seem to have any eff ...