错误nodejs要求它没有定义

时间:2018-02-04 14:38:48

标签: javascript html node.js

嗨,当我运行本地服务时出现此错误 感谢

code html
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title></title>
  </head>
  <body>

    <label>Your ID:</label><br/>
    <textarea id="yourId"></textarea><br/>
    <label>Other ID:</label><br/>
    <textarea id="otherId"></textarea>
    <button id="connect">connect</button><br/>

    <label>Enter Message:</label><br/>
    <textarea id="yourMessage"></textarea>
    <button id="send">send</button>
    <pre id="messages"></pre>

    <script src="index.js" ></script>
  </body>
</html>

index.js

var getUserMedia = require('getusermedia')

getUserMedia({ video: true, audio: false }, function (err, stream) {
  if (err) return console.error(err)

  var Peer = require('simple-peer')
  console.log("conectar")
  var peer = new Peer({
    initiator: location.hash === '#init',
    trickle: false,
    stream: stream

  })

  peer.on('signal', function (data) {
    document.getElementById('yourId').value = JSON.stringify(data)
    console.log("peer on signal 1")
    console.log(data)

  })

  document.getElementById('connect').addEventListener('click', function () {
    var otherId = JSON.parse(document.getElementById('otherId').value)
    console.log("conectar")
    peer.signal(otherId)
  })

  document.getElementById('send').addEventListener('click', function () {
    var yourMessage = document.getElementById('yourMessage').value
    console.log("getElementById")
    peer.send(yourMessage)
  })

  peer.on('data', function (data) {
    console.log("peer data")
    document.getElementById('messages').textContent += data + '\n'
  })

  peer.on('stream', function (stream) {
    console.log("stream")
    var video = document.createElement('video')
    document.body.appendChild(video)

    video.src = window.URL.createObjectURL(stream)
    video.play()
  })
})

server.js

var https = require('https');
var fs = require('fs');




var options = {
  key: fs.readFileSync('privateKey.key'),
  cert: fs.readFileSync('certificate.crt')
};

var mundo= https.createServer(options, function (req, res) {

  console.log(req.url)



  if(req.url === '/') {
    res.writeHead(200, {'Content-Type': 'text/html'});
    res.end(fs.readFileSync("./index.html"));
  }

  if(req.url === '/index.js') {
  res.writeHead(200, {'Content-Type': 'application/javascript'});
    res.end(fs.readFileSync("./index.js"));
    };


  if(req.url === '/favicon.ico') {
    res.end(fs.readFileSync("./favicon.ico"));
    };


  res.end();
  console.log("listening to port 8000");

});


mundo.listen(8443);

this is the error

1 个答案:

答案 0 :(得分:0)

您在客户端脚本中使用require(在index.js中)。浏览器不支持require。

查看here了解详情。