Home > Blockchain >  I am working on a query string , and i get this: Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers a
I am working on a query string , and i get this: Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers a

Time:10-02

When i type: "localhost:8080/search?q=something" on the browser => i render this: Search Results for: something The point is that i'm trying to output "NOTHING FOUND IF NOTHING SEARCHED" when there is no query string(just: /search)

Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
    at ServerResponse.setHeader (_http_outgoing.js:485:11)
    at ServerResponse.header (E:\Programming space\udemy\Web developer bootcamp\Express
\FirstApp\node_modules\express\lib\response.js:771:10)
    at ServerResponse.contentType (E:\Programming space\udemy\Web developer bootcamp\Ex
press\FirstApp\node_modules\express\lib\response.js:599:15)
    at ServerResponse.send (E:\Programming space\udemy\Web developer bootcamp\Express\F
irstApp\node_modules\express\lib\response.js:145:14)
    at E:\Programming space\udemy\Web developer bootcamp\Express\FirstApp\index.js:45:9
    at Layer.handle [as handle_request] (E:\Programming space\udemy\Web developer bootc
amp\Express\FirstApp\node_modules\express\lib\router\layer.js:95:5)
    at next (E:\Programming space\udemy\Web developer bootcamp\Express\FirstApp\node_mo
dules\express\lib\router\route.js:137:13)
    at Route.dispatch (E:\Programming space\udemy\Web developer bootcamp\Express\FirstA
pp\node_modules\express\lib\router\route.js:112:3)
    at Layer.handle [as handle_request] (E:\Programming space\udemy\Web developer bootc
amp\Express\FirstApp\node_modules\express\lib\router\layer.js:95:5)
    at E:\Programming space\udemy\Web developer bootcamp\Express\FirstApp\node_modules\
express\lib\router\index.js:281:22

This my code:

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


app.get('/search', (req, res) => {
    const { q } = req.query;
    if(!q) { 
        res.send('NOTHING FOUND IF NOTHING SEARCHED')  
    } 
    res.send(`<h1>Search results for: ${q}</h1>`) 
})

app.listen(port, () => {
console.log(`Listening On Port ${port}...`);  
}) 

CodePudding user response:

You can't send two or more responses to client.

Getting like this your code:

app.get('/search', (req, res) => {
  const { q } = req.query;
  if(!q) { 
    return res.send('NOTHING FOUND IF NOTHING SEARCHED')  
  } 
  res.send(`<h1>Search results for: ${q}</h1>`) 
})

or

app.get('/search', (req, res) => {
  const { q } = req.query;
  if(!q) { 
    res.send('NOTHING FOUND IF NOTHING SEARCHED')  
  } else {
    res.send(`<h1>Search results for: ${q}</h1>`) 
  }  
})

CodePudding user response:

This happens because if there is no search the server sends 'NOTHING FOUND IF NOTHING SEARCHED' from inside the if clause as expected. The function continues after that and tries to execute the second res.send().

To solve that you could either return res.send('NOTHING FOUND IF NOTHING SEARCHED') or you can add an else clause. Here an example with an else clause.

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


app.get('/search', (req, res) => {
    const { q } = req.query;
    if(!q) { 
        res.send('NOTHING FOUND IF NOTHING SEARCHED')  
    } else { 
        res.send(`<h1>Search results for: ${q}</h1>`) 
    }
})

app.listen(port, () => {
console.log(`Listening On Port ${port}...`);  
})

  • Related