2014-04-19 7 views
11

Piszę mój pierwszy serwer sieciowy w węźle (proszę dać mi informację zwrotną), który używa ekspresu i gith do wdrożenia. Problem polega na tym, że po wdrożeniu otrzymuję poniższy błąd, a jeśli uruchomię skrypt za pośrednictwem pm2, proces pozornie zostanie usunięty. Jakieś pomysły? Skrypt jest załączony poniżej.Serwer ekspresowy: Błąd: żądany zakres nie jest satysfakcjonujący.

sudo node server.js 
Error: Requested Range Not Satisfiable 
    at SendStream.error (/home/gareth/node_modules/express/node_modules/send/lib/send.js:145:16) 
    at SendStream.send (/home/gareth/node_modules/express/node_modules/send/lib/send.js:371:19) 
    at /home/gareth/node_modules/express/node_modules/send/lib/send.js:323:10 
    at /home/gareth/node_modules/newrelic/node_modules/continuation-local-storage/node_modules/async-listener/glue.js:177:31 
    at Object.oncomplete (fs.js:107:15) 
Completed deployment 

Server.js

/* 
    Webserver 
     Should use port 80 on production 
     Mongo DB for database - http://docs.mongodb.org/manual/tutorial/ 
     Use pm2 to ensure it runs forever and load balanced 

    NPM 
     https://www.npmjs.org/package/fs 
     https://www.npmjs.org/package/express 
     https://www.npmjs.org/package/body-parser 
     https://www.npmjs.org/package/gith 
     https://www.npmjs.org/package/nodemailer 
*/ 

// server monitoring 
require('newrelic'); 

var APP = { 

    // include some scripts 
    express: require('express'), 
    fs: require('fs'), 
    mdb: require('mongodb'), 
    nodemailer: require('nodemailer'), 
    gith: require('gith').create(9001), 
    execFile: require('child_process').execFile, 

    // setup 
    port: 80, 
    dbUrl: 'mongodb://127.0.0.1:27017/test', 
    gitRepo: '*****', 
    gmailUser: '*****', 
    gmailPass: '******', 
    email: '*****', 
    subject: 'Website enquiry', 
    linuxUser: '*****', 
    wwwPath: '/var/www/', 

    // vars 
    server: null, 
    app: null, 
    options: {}, 
    smtpTransport: null, 
    db: null, 

    init: function(){ 

     // setup express 
     APP.app = APP.express().use(require('body-parser')()); 

     // create the server 
     APP.fs.exists('./ssl/privatekey.pem', function (e){ 
      if(e){ 
       APP.fs.exists('./ssl/certificate.pem', function (e){ 
        if(e){ 
         APP.options = { 
          key: APP.fs.readFileSync('./ssl/privatekey.pem'), 
          cert: APP.fs.readFileSync('./ssl/certificate.pem'), 
         }; 
         APP.server = require('http').createServer(APP.options, APP.app).listen(APP.port, '0.0.0.0'); 
        } else { 
         APP.server = require('http').createServer(APP.app).listen(APP.port, '0.0.0.0'); 
        } 
       }); 
      } else { 
       APP.server = require('http').createServer(APP.app).listen(APP.port, '0.0.0.0'); 
      } 
     }); 

     // set up smtp 
     APP.smtpTransport = APP.nodemailer.createTransport('Gmail',{ 
      auth: { 
       user: APP.gmailUser, 
       pass: APP.gmailPass, 
      } 
     }); 

     // http routing 
     APP.routing(); 

     // wait for github push 
     APP.gith({ 
      repo: APP.gitRepo 
     }).on('all', function(payload){ 
      if(payload.branch === 'master' && payload.original.commits[0].message.indexOf('#deploy') >= 0){ 

       APP.execFile('/home/'+APP.linuxUser+'/deploy.sh', function(err, stdout, stderr) { 
        console.log('Completed deployment'); 
       }); 

      } 
     }); 

    }, 

    // open the db 
    openDB: function(){ 

     APP.mdb.connect(APP.dbURL, function(err, db){ 

      if(err) 
       throw err; 

      APP.db = db; 

     }); 

    }, 

    // close the db 
    closeDB: function(){ 

     APP.db.close(); 

    }, 

    // insert a file to the db 
    create: function (col, data){ 

     // open the db 
     APP.openDB(); 

     var collection = APP.db.collection(col); 
     collection.insert(data, function(err, docs){ 
      if(err){ 
       console.warn(err.message); 
      } else { 
       console.log('Successfully inserted record'); 
      } 
     }); 

     // close the db 
     APP.closeDB(); 

    }, 

    // insert a file to the db 
    update: function (col, crit, data){ 

     // open the db 
     APP.openDB(); 

     // example criteria 
     // {key: value} // get something specific 
     // {key: {$lt: value}} // Less Than 
     // {key: {$gte: value}} // Greater than or equal to 
     // {key: {$ne: 'value'}} // Not Equal To 
     // {key: {$in: ['value', 'value', 'value']}} // Exists in array 

     // updateoperators 
     // db.col.update({key: 'value'}, {$addToSet: {key: ['value']}}); 
     // Or we can add a new field to Cash 
     // db.col.update({key: 'value'}, {$set: {'age': 50} }); 
     // You can also push and pull items from arrays: 
     // db.col.update({key: 'value'}, {$push: {'key': 'value'} }); 
     // db.col.update({key: 'value'}, {$pull: {'key': 'value'} }); 

     var collection = APP.db.collection(col); 
     collection.update(crit, data, function(){ 
      if(err){ 
       console.warn(err.message); 
      } else { 
       console.log('Successfully updated record'); 
      } 
     }); 

     // close the db 
     APP.closeDB(); 

    }, 

    // find all in the db collection that match 
    read: function (col, crit){ 

     // open the db 
     APP.openDB(); 

     // example criteria 
     // {key: value} // get something specific 
     // {key: {$lt: 5}} // Less Than 
     // {key: {$gte: 10}} // Greater than or equal to 
     // {key: {$ne: 'b'}} // Not Equal To 
     // {key: {$in: ['a', 'b', 'c']}} // Exists in array 

     var collection = APP.db.collection(col); 
     collection.find(crit).toArray(function(err, results) { 
      if(err){ 
       console.warn(err.message); 
      } else { 
       console.log(results); 
      } 
     }); 

     // close the db 
     APP.closeDB(); 

    }, 

    // find and delete from collection 
    delete: function (col, crit){ 

     // open the db 
     APP.openDB(); 

     // example criteria 
     // {key: value} // get something specific 
     // {key: {$lt: 5}} // Less Than 
     // {key: {$gte: 10}} // Greater than or equal to 
     // {key: {$ne: 'b'}} // Not Equal To 
     // {key: {$in: ['a', 'b', 'c']}} // Exists in array 

     var collection = APP.db.collection(col); 
     collection.remove(crit); 

     // close the db 
     APP.closeDB(); 

    }, 

    // routing files 
    routing: function(){ 

     // hide the engine creating the server 
     APP.app.disable('x-powered-by'); 

     // index page 
     APP.app.get('/*', function (req, res, next){ 
      APP.renderPage(req, res, next); 
     }); 

     // handle post data 
     APP.app.post('/', function(req, res, next) { 
      APP.sendEmail(req); 
      APP.renderPage(req, res, next); 
     }); 

     APP.app.post('/index.html', function(req, res, next) { 
      APP.sendEmail(req); 
      APP.renderPage(req, res, next); 
     }); 

    }, 

    // render page 
    renderPage: function (req, res, next){ 

     // get the host and point to correct folder 
     var host = req.get('host'); 
     if(host.indexOf('www.') >= 0){ 
      host = host.replace('www.', ''); 
     } 

     // see if we are on staging 
     if(host.indexOf('staging.') >= 0){ 

      // tidy url 
      host = host.replace('staging.', ''); 

      var url = APP.wwwPath + host + '/app'; 
      APP.fs.exists(url + req.url, function (e){ 
       if(e){ 
        res.sendfile(url + req.url); 
       } else { 
        res.sendfile(url + '/404.html'); 
       } 
      }); 

     } else { 

      var url = APP.wwwPath + host + '/dist'; 
      APP.fs.exists(url + req.url, function (e){ 
       if(e){ 
        res.sendfile(url + req.url); 
       } else { 
        res.sendfile(url + '/404.html'); 
       } 
      }); 

     } 

    }, 

    // sanitize post 
    sendEmail: function (req){ 

     var name = req.body.name, 
     email = req.body.email, 
     msg = req.body.msg, 
     nameRegex = /^([ \u00c0-\u01ffa-zA-Z'\-])+$/, 
     emailRegex = /^(([^<>()[\]\\.,;:\[email protected]\']+(\.[^<>()[\]\\.,;:\[email protected]\']+)*)|(\'.+\'))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/; 

     if(emailRegex.test(email) && nameRegex.test(name) && msg.length > 10){ 

      // setup e-mail data with unicode symbols 
      var mailOptions = { 
       from: name + ' <' + email + '>', 
       to: APP.email, 
       subject: APP.subject, 
       text: msg, 
       html: '<p>'+ msg +'</p>' 
      } 

      // send mail with defined transport object 
      APP.smtpTransport.sendMail(mailOptions, function(error, response){ 
       if(error){ 
        console.log(error); 
       }else{ 
        console.log('Message sent: ' + response.message); 
       } 

       // shut down the connection pool - no more messages 
       //smtpTransport.close(); 
      }); 

     } 

    } 

}; 

// run the script 
APP.init(); 
+0

Nie jestem pewien, co się wydarzyło, ale to działa, kiedy przerwać dwóch instancji serwera lub funkcji z tego samego pliku. Mam teraz dwie instancje plików/serwera działające na różnych portach na serwerze. – gazzwi86

+0

Mam prostą aplikację Express, która zawiesza ten błąd. – Randomblue

+0

Czy możesz zamieścić źródło na github lub gdzieś? Chciałbym, żeby całe źródło się rozmnażało. – Vinnyq12

Odpowiedz

1

nie powinno się używać require('https')? Zbadaj także, czy istnieje express.static ...

1

Wygląda na to, że błąd pochodzi ze skryptu deploy.sh.

Aby to naprawić, upewnij się, że żądania wychodzące nie mają wartości Content-Range.

również może pomóc, aby opublikować deploy.sh

0

spróbować przechodzących acceptRanges: false jako argument do metody res.sendFile.

Więc zamiast dzwonić res.sendFile(filePath) użycie res.sendFile(filePath, {acceptRanges: false})

Powiązane problemy