Search code examples
node.jsexpresspathanchor

Node Express path hash anchor


I currently have the following url structure in my Node application:

http://localhost:3000/#record_2

Clicking on a link like the above will scroll the webpage to the element with the matching id of record_2.

But I need extra information which can be detected and used in my Express path. I have tried the following:

http://localhost:3000/#record_2?test=2
http://localhost:3000/#record_2/test/2

However, my path is not recognised using the following code :

app.get('/#:record/test/:id', async (request, response) => {
    console.log('Request: ', request.params)
    response.redirect('/')
})

app.get('/#:record?test=:id', async (request, response) => {
    console.log('Request: ', request.params)
    response.redirect('/')
})

I don't need the #record_??? segment to be processed on the server. How can I get the test part back to my server code for processing?


Solution

  • Path segments and query options must become before the hash symbol: http://localhost:3000/?test=2#record_2 or http://localhost:3000/test/2/#record_2.