Uncaught ReferenceError: require is not defined

2019-07-18 18:56发布

问题:

I am trying to get the video uploader to work on my server i have node install on my server as root.

i have installed the following in the folder where my node js file is

cd /assets/js
npm install socket.io
npm install requirejs

Then i run the node file with

]# node uploader.js                              
 info  - socket.io started
 debug - served static content /socket.io.js

and here is my uploader.js

    var app = require('http').createServer(handler)
  , io = require('socket.io').listen(app)
  , fs = require('fs')
  , exec = require('child_process').exec
  , util = require('util')
  , Files = {};

app.listen(8080);

function handler (req, res) {
  fs.readFile(__dirname + '/index.html',
  function (err, data) {
    if (err) {
      res.writeHead(500);
      return res.end('Error loading index.html');
    }
    res.writeHead(200);
    res.end(data);
  });
}

io.sockets.on('connection', function (socket) {
    socket.on('Start', function (data) { //data contains the variables that we passed through in the html file
            var Name = data['Name'];
            Files[Name] = {  //Create a new Entry in The Files Variable
                FileSize : data['Size'],
                Data     : "",
                Downloaded : 0
            }
            var Place = 0;
            try{
                var Stat = fs.statSync('Temp/' +  Name);
                if(Stat.isFile())
                {
                    Files[Name]['Downloaded'] = Stat.size;
                    Place = Stat.size / 524288;
                }
            }
            catch(er){} //It's a New File
            fs.open("Temp/" + Name, 'a', 0755, function(err, fd){
                if(err)
                {
                    console.log(err);
                }
                else
                {
                    Files[Name]['Handler'] = fd; //We store the file handler so we can write to it later
                    socket.emit('MoreData', { 'Place' : Place, Percent : 0 });
                }
            });
    });

    socket.on('Upload', function (data){
            var Name = data['Name'];
            Files[Name]['Downloaded'] += data['Data'].length;
            Files[Name]['Data'] += data['Data'];
            if(Files[Name]['Downloaded'] == Files[Name]['FileSize']) //If File is Fully Uploaded
            {
                fs.write(Files[Name]['Handler'], Files[Name]['Data'], null, 'Binary', function(err, Writen){
                    var inp = fs.createReadStream("Temp/" + Name);
                    var out = fs.createWriteStream("Video/" + Name);
                    util.pump(inp, out, function(){
                        fs.unlink("Temp/" + Name, function () { //This Deletes The Temporary File
                            exec("ffmpeg -i Video/" + Name  + " -ss 01:30 -r 1 -an -vframes 1 -f mjpeg Video/" + Name  + ".jpg", function(err){
                                socket.emit('Done', {'Image' : 'Video/' + Name + '.jpg'});
                            });
                        });
                    });
                });
            }
            else if(Files[Name]['Data'].length > 10485760){ //If the Data Buffer reaches 10MB
                fs.write(Files[Name]['Handler'], Files[Name]['Data'], null, 'Binary', function(err, Writen){
                    Files[Name]['Data'] = ""; //Reset The Buffer
                    var Place = Files[Name]['Downloaded'] / 524288;
                    var Percent = (Files[Name]['Downloaded'] / Files[Name]['FileSize']) * 100;
                    socket.emit('MoreData', { 'Place' : Place, 'Percent' :  Percent});
                });
            }
            else
            {
                var Place = Files[Name]['Downloaded'] / 524288;
                var Percent = (Files[Name]['Downloaded'] / Files[Name]['FileSize']) * 100;
                socket.emit('MoreData', { 'Place' : Place, 'Percent' :  Percent});
            }
        });
});

My client side code is

<div id="UploadBox">
      <h2>Video Uploader</h2>
      <span id='UploadArea'>
         <label for="FileBox">Choose A File: </label><input type="file" id="FileBox"><br>
         <label for="NameBox">Name: </label><input type="text" id="NameBox"><br>

         <button  type='button' id='UploadButton' class='Button'>Upload</button>
      </span>
   </div>
<script src="http://example.com:8080/socket.io/socket.io.js"></script>
<script type="text/javascript" src="<?php echo base_url(); ?>assets/js/uploader.js?a=346456" type="text/javascript" ></script>

But when i then run in my browser i get Uncaught ReferenceError: require is not defined

Any help on this please