Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Express server: Error: Requested Range Not Satisfiable

Im writing my first node web server (feel free to give me feedback) that uses express and gith to deploy. The problem is I'm receiving the below error once deploying, and if I run the script via pm2 the process seemingly gets deleted. Any ideas? The script is attached below.

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 = /^(([^<>()[\]\\.,;:\s@\']+(\.[^<>()[\]\\.,;:\s@\']+)*)|(\'.+\'))@((\[[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();
like image 797
gazzwi86 Avatar asked Apr 19 '14 14:04

gazzwi86


2 Answers

Shouldn't you use require('https')? Also investigate express.static...

like image 118
malix Avatar answered Nov 20 '22 04:11

malix


Looks like the error is coming out of your deploy.sh script.

To fix this, make sure that outgoing requests do not have the Content-Range.

Also might help to post the deploy.sh

like image 1
Belfordz Avatar answered Nov 20 '22 03:11

Belfordz