How do i put "search/?q=%s" in my url

Question:
how do i put this in my URL search/?q=%s

1 Like

Are you trying to implement a query functionality within the search route?

3 Likes

yes i want ‘search/?q=%s’ so i can use my own search engine even tho it ant a search engine

1 Like

I believe you need your code to be in a node.js Repl.

2 Likes

It’s possible with just HTML/CSS/JS, but yeah it makes more sense to have a backend

2 Likes

but how would i do it

well here’s how you’d do it with node.js

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

app.get('/search', (req, res) => {
  const query = req.query.q;
  // Do something with the query
  if (query && query === "cats") {
    res.status(200).json([
      {
        title: 'Cats Are Cool',
        description: 'This website is all about cats and cats are cool',
        url: 'catsarecool.com'
      },
      {
        title: 'Cats funny videos',
        description: 'Videos all about cats and they are funny!',
        url: 'catsfunnyvideos.com'
      }
    ]);
  }
});

app.listen(3000);

now go to /search?q=cats and you can see it returns json

3 Likes

how would you do it on the old html/css/java

I said it’s possible but… don’t. Just complicates things. And it’s JavaScript not Java.

4 Likes

sorry but you know what i mean lol thanks I will leave this open tell me if you fined away to do it on html

2 Likes

There is a way to do it with HTML, but it’s better to just use a server.

But, if you really want to know, here’s how.

let querystring = window.location.search;
let query = querystring.split("?q=")[1].split("&")[0];

function search(q) {
  // handle searching
}

search(query);
2 Likes

uh

function search(query) {
  // handle searching
}

let urlString = (window.location.href).toLowerCase();
let url = new URL(urlstring);
let query = url.searchParams.get("q");

search(query);

but now I realize that you need to return json so Idk if that’s possible here

4 Likes

best way I do it is:

const query = location.href.split("?q=")[1]

and by chance if you will have more parameters in the URL, I’d say

const query = location.href.split("?q=")[1].split("&")[0]

works well. But that might get really confusing, especially if you’re working with multiple parameters. The less confusing way is definitely URLSearchParams or something like it.

There are way better solutions above me, but this is usually just my go-to one-liner. It even allows for you to just do a blank parameter, like this:
mywebsite.com/eventDetails?eventID
or something like that, and then when you do

const param = location.href.split("?")[1]

then the result is "eventID"

But yeah

can you make me a replit and show me please i am counfused

1 Like

https://replit.com/@boston2029/CyberUnsungProcessor

if you go to https://cyberunsungprocessor.boston2029.repl.co/search?q=MyQuery

then it will display MyQuery on the screen. It could be tweaked to your use case but that’s a really basic way

The best way to do it is by using the built-in URLSearchParams.

new URLSearchParams(location.search).get('q')
2 Likes