title: how to fix "xinit: unable to connect to X server" in nodejs exec
body: <p>My Xserver does not start from the nodejs script. I have to run it as root because otherwise the following error occurs: </p>
  
  <blockquote>
    <p>/usr/lib/xorg/Xorg.wrap: Only console users are allowed to run the X server</p>
  </blockquote>
  
  <p>so I start the script as</p>
  
  <blockquote>
    <p>sudo node controller.js</p>
  </blockquote>
  
  <p>First I start my controller to run a locale webserver. Then I want to start my Xorg server and run my chromium-browser per xinit command.</p>
  
  <pre><code>const http = require('http');
  const fs = require('fs');
  const path = require('path');
  const config = require('./config.json');
  const shell = require('shelljs');
  
  http.createServer(function (request, response) {
      var filePath = '.' + request.url;
      if(filePath == './'){
          filePath = config.baseFile;
      }
      var extname = path.extname(filePath);
      var contentType='text/html';
      switch(extname){
          case '.js':
              contentType = 'text/javascript';
              break;
          case '.css':
              contentType = 'text/css';
              break;
          case '.json':
              contentType = 'application/json';
              break;
          case '.png':
              contentType = 'image/png';
              break;      
          case '.jpg':
              contentType = 'image/jpg';
              break;
          case '.wav':
              contentType = 'audio/wav';
              break;
      }
      fs.readFile(filePath, function(error, content) {
          if (error) {
              if(error.code == 'ENOENT'){
                  fs.readFile('./404.html', function(error, content) {
                      response.writeHead(200, { 'Content-Type': contentType });
                      response.end(content, 'utf-8');
                  });
              }
              else {
                  response.writeHead(500);
                  response.end('Sorry, check with the site admin for error: '+error.code+' ..\n');
                  response.end(); 
              }
          }
          else {
              response.writeHead(200, { 'Content-Type': contentType });
              response.end(content, 'utf-8');
          }
      });
  }).listen(8080);
  console.log('Server running at http://127.0.0.1:8080/');
  shell.exec('xinit chromium-browser --start-maximized -kiosk http://localhost:8080/');
  </code></pre>
  
  <blockquote>
    <p>(EE)<br> Fatal server error: <br> (EE) Server is already active for
    display 0<br> If this server is no longer running, remove
    /tmp/.X0-lock and start again.<br> (EE)<br> (EE)<br> Please consult
    the The X.Org Foundation support at <a href="http://wiki.x.org" rel="nofollow noreferrer">http://wiki.x.org</a> for help.<br>
    (EE)<br> No protocol specified<br> xinit: giving up<br> xinit: unable
    to connect to X server: Resource temporarily unavailable<br> xinit:
    server error</p>
  </blockquote>
  
  <p>I deleted this file multiple times. I use the raspberry stretch lite distro.</p>
  
  <blockquote>
    <p>$ ps auxw | grep X<br>
    pi 949 0.0 0.0 3536 624 tty1 S+ 16:38 0:00 xinit /etc/X11/xinit/xinitrc -- /etc/X11/xinit/xserverrc :0 vt1 -keeptty -auth /tmp/serverauth.wsn7HgMgWE <br>
    root 949 0.0 3.9 182112 37440 tty1 S1 16:38 0:00 /usr/lib/xorg/Xorg -nolisten tcp :0 vt1 -keeptty -auth /tmp/serverauth.wsn7HgMgWE <br>
    pi 1188 0.0 0.0 4772 572 tty5 R+ 17:18 0:00 grep --color=auto X</p>
  </blockquote>