title: How to get value of URL parameter in Node.js?
body: <p>For example I have this URL: <code>http://localhost/chat.html?channel=talk</code>
  How can I get the value of parameter <code>channel</code> in Node.js?
  I want to store the value of <code>channel</code> in a variable.</p>
  
  <p>I changed server.get to this: </p>
  
  <pre><code>server.get("/channel", (req, res) =&gt; {
      let query = url.parse(req.url, true).query;
      console.log(req.query.channel);
      let rueckgabe = {
          channel: req.query.channel
      };
      res.send(JSON.stringify(rueckgabe));
  });
  </code></pre>
  
  <p>Now I'm expecting an output of the value of <code>channel</code> on my console but nothing appears.</p>
  
  <p>This is the full code of <code>index.js</code>:</p>
  
  <pre><code>//Server erstellen
  const express = require("express");
  let server = express();
  server.use(express.static("public"));
  
  //Socket.io
  const http = require("http");
  let httpServer = http.Server(server);
  const socketIo = require("socket.io");
  let io = socketIo(httpServer);
  
  //Eventlistener bei Verbindungsaufbau
  io.on("connection", (socket) =&gt; {
      console.log(socket.id);
  
      socket.on("chatnachricht", eingabe =&gt; {
          io.emit("nachricht", eingabe);
      });
  });
  
  let stdIn = process.openStdin();
  stdIn.addListener("data", (eingabe) =&gt; {
      io.emit("nachricht", eingabe.toString());
  });
  
  server.get("/channel", (req, res) =&gt; {
      let query = url.parse(req.url, true).query;
      console.log(query);
      let rueckgabe = {
          channel: query.channel
      };
      //res.send(JSON.stringify(rueckgabe));
      res.send(JSON.stringify(rueckgabe));
  });
  
  httpServer.listen(80, () =&gt; {
      console.log("Server läuft");
  });
  </code></pre>
  
  <h1>SOLUTION</h1>
  
  <p>This code works so far but with limitations:</p>
  
  <pre><code>//Server erstellen
  const express = require("express");
  let server = express();
  server.use(express.static("public"));
  
  const http = require("http");
  let httpServer = http.Server(server);
  const socketIo = require("socket.io");
  let io = socketIo(httpServer);
  var router = express.Router();
  const url = require("url");
  var path = require('path');
  
  
  //Eventlistener bei Verbindungsaufbau
  io.on("connection", (socket) =&gt; {
      console.log(socket.id);
  
      socket.on("chatnachricht", eingabe =&gt; {
          io.emit("nachricht", eingabe);
      });
  });
  
  /*
  let stdIn = process.openStdin();
  stdIn.addListener("data", (eingabe) =&gt; {
      io.emit("nachricht", eingabe.toString());
  });
  */
  
  server.get("/chat", (req, res) =&gt; {
      let query = url.parse(req.url, true).query;
      console.log(query.channel);
      let rueckgabe = {
          channel: query.channel
      };
      res.sendFile('chat.html', { root: path.join(__dirname, 'public/') });
      //res.send(JSON.stringify(rueckgabe));
  });
  
  httpServer.listen(80, () =&gt; {
      console.log("Server läuft");
  });
  </code></pre>
  
  <p>Now it works with <code>server.get()</code> but I can't use both <code>res.sendFile('chat.html', { root: path.join(__dirname, 'public/') });</code> and <code>res.send(JSON.stringify(rueckgabe));</code>. How can I use both?</p>