Sunday, 16 April 2017

Facebook API: read_mailbox

I'm trying to build a web application for managing facebook messages, send and receive messages..., using Angular and Nodejs, in which I have to use facebook read_mailbox permission, but the problem is that this permission in deprecated, and no longer accepted for web apps. So any chance I can do the same thing with another permission? PS: I'm not trying to build a bot for a page. Thanks,



via Kml Hr

should i have to use error method in every callback function in express?

here is my code for my express application

app.listen(port, function(){console.log("Connected "+port);})

But when start server it shows error as "Unhandled 'error' event" at event.js:163 How to resolve it? should i have to use error as parameter on every callback function?



via Jana

GET /dishes 500 4.732 ms - - Error: Can't set headers after they are sent

I am new to Node.js,and so far I have managed to install Express. Now I have three js files,that handle CRUD requests to a localserver.

ie.

localhost:3000/dishes
localhost:3000/promotions

etc. However, when I am doing a simple get request in postman,I get this error.

GET /dishes 500 8.883 ms - -
Error: Can't set headers after they are sent.
at ServerResponse.OutgoingMessage.setHeader (_http_outgoing.js:356:11)

I am going to show you my files.

dishRouter.js

//exporing the functionality
module.exports = function (dishRouter, bp) {
dishRouter.use(bp.json()); //using body parser for parsing json from body 
content
//code for url '/'
dishRouter.route('/').all(function (req, res, next) {
        res.writeHead(200, {
            'Content-Type': 'text/plain'
        });
        next();
    })
    //setting up code for get request
    .get(function (req, res, next) {
        res.end('Will send all the dishes to you!');
    })
    //setting up code for post request
    .post(function (req, res, next) {
        res.end('Will add the dish: ' + req.body.name + ' with details: ' + 
    req.body.description);
    })
    //setting up code for delete request
    .delete(function (req, res, next) {
        res.end('Deleting all dishes');
    });
//code for url with specific ids
dishRouter.route('/:dishId')
    .all(function (req, res, next) {
        res.writeHead(200, {
            'Content-Type': 'text/plain'
        });
        next();
    })
    //setting up code for get request
    .get(function (req, res, next) {
        res.end('Will send details of the dish: ' + req.params.dishId + ' to you!');
    })
    //setting up code for put request
    .put(function (req, res, next) {
        res.write('Updating the dish: ' + req.params.dishId + '\n');
        res.end('Will update the dish: ' + req.body.name +
            ' with details: ' + req.body.description);
    })
    //setting up code for delete request
    .delete(function (req, res, next) {
        res.end('Deleting dish: ' + req.params.dishId);
    });

 };

promoRouter.js

   //exporting the functionality
    module.exports = function (promoRouter, bp) {
        promoRouter.use(bp.json()); //using body parser for parsing json 
    from body content
        //code for url '/'
        promoRouter.route('/').all(function (req, res, next) {
                res.writeHead(200, { //writing header
                    'Content-Type': 'text/plain'
                });
                next();
            })
            //setting up code for get request
            .get(function (req, res, next) {
                res.end('Will send all the promotions to you!');
            })
            //setting up code for post request
            .post(function (req, res, next) {
                res.end('Will add the promotion: ' + req.body.name + ' with 
    details: ' + req.body.description);
            })
            //setting up code for delete request
            .delete(function (req, res, next) {
                res.end('Deleting all promotions');
            });
        //code for url with specific ids
        promoRouter.route('/:promoId')
            .all(function (req, res, next) {
                res.writeHead(200, {
                    'Content-Type': 'text/plain'
                });
                next();
            })
            //setting up code for get request
            .get(function (req, res, next) {
                res.end('Will send details of the promotion: ' + 
    req.params.promoId + ' to you!');
            })
            //setting up code for put request
            .put(function (req, res, next) {
                res.write('Updating the promotion: ' + req.params.promoId + '\n');
                res.end('Will update the promotion: ' + req.body.name +
                    ' with details: ' + req.body.description);
            })
            //setting up code for delete request
            .delete(function (req, res, next) {
                res.end('Deleting promotion: ' + req.params.promoId);
            });
    };

leaderRouter.js

//exporing the functionality
module.exports = function (leaderRouter, bp) {
leaderRouter.use(bp.json()); //using body parser for parsing json from body 
 content
//code for url '/'
leaderRouter.route('/').all(function (req, res, next) {
        res.writeHead(200, {
            'Content-Type': 'text/plain'
        });
        next();
    })
    //setting up code for get request
    .get(function (req, res, next) {
        res.end('Will send all the leaders to you!');
    })
    //setting up code for post request
    .post(function (req, res, next) {
        res.end('Will add the leader: ' + req.body.name + ' with details: ' 
   + req.body.description);
    })
    //setting up code for delete request
    .delete(function (req, res, next) {
        res.end('Deleting all leaders');
    });
//code for url with specific ids
leaderRouter.route('/:leaderId')
    .all(function (req, res, next) {
        res.writeHead(200, {
            'Content-Type': 'text/plain'
        });
        next();
    })
    //setting up code for get request
    .get(function (req, res, next) {
        res.end('Will send details of the leader: ' + req.params.leaderId + ' to you!');
    })
    //setting up code for put request
    .put(function (req, res, next) {
        res.write('Updating the leader: ' + req.params.leaderId + '\n');
        res.end('Will update the leader: ' + req.body.name +
            ' with details: ' + req.body.description);
    })
    //setting up code for delete request
    .delete(function (req, res, next) {
        res.end('Deleting leader: ' + req.params.leaderId);
    });
 };

The above 3 files are inside the routes folder. And finally,I have app.js.

var express = require('express');
    var path = require('path');
    var favicon = require('serve-favicon');
    var logger = require('morgan');
    var cookieParser = require('cookie-parser');
    var bodyParser = require('body-parser');

    var routes = require('./routes/index');
    var users = require('./routes/users');
    var dishRouter = require('./routes/dishRouter');
    var promoRouter = require('./routes/promoRouter');
    var leaderRouter = require('./routes/leaderRouter');

    var app = express();

    // view engine setup
    app.set('views', path.join(__dirname, 'views'));
    app.set('view engine', 'jade');

    // uncomment after placing your favicon in /public
    //app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));

    app.use(logger('dev'));
    app.use(bodyParser.json());
    app.use(bodyParser.urlencoded({ extended: false }));
    app.use(cookieParser());

    app.use(express.static(path.join(__dirname, 'public')));

    app.use('/', routes);
    app.use('/users', users);
    app.use('/dishes',dishRouter);
    app.use('/promotions',promoRouter);
    app.use('/leadership',leaderRouter);

    // catch 404 and forward to error handler
    app.use(function(req, res, next) {
      var err = new Error('Not Found');
      err.status = 404;
      next(err);
    });

    // error handlers
    // development error handler
    // will print stacktrace
    if (app.get('env') === 'development') {
      app.use(function(err, req, res, next) {
        res.status(err.status || 500);
        res.render('error', {
          message: err.message,
          error: err
        });
      });
    }

    // production error handler
    // no stacktraces leaked to user
    app.use(function(err, req, res, next) {
      res.status(err.status || 500);
      res.render('error', {
        message: err.message,
        error: {}
      });
    });

    module.exports = app;

Any ideas why I am getting this error? Please advice.

Thanks,

Theo.



via Theo

React configuration problems (Node, expres, webpack, mongo .... )

So I posted a qustion some time ago asking for webpack configuration, everythng is good with it now but still React js wont work in my app.

added the bundl.js in my about.ejs but no "hello world" for me

Here's my repo : https://github.com/LAsmaa/TeamDb

When i run it locally i get no errors in the console nothing .

I'm working on windows. Is it a problem ?



via Asmaa

get file path using fetch API on cilent side?

I used aws-sdk in node.js,

s3BucketMgt=require("s3-nodejs-sample");
var config=require("./config.js");
var bucketName="node-sdk-sample-cbt-003";
var fileToUpload = "D:/gitHub/awt-nodejs-sample/s3-nodejs-sample/logo.jpg";
s3BucketMgt.uploadFile(bucketName,fileToUpload,"null",config,function(data){
 if(data.status==false){
  console.log("Error in Upload file:"+data.error);
 }else{
  console.log("File Uploaded in Bucket.");
 }
});

it required a file path from the cilent side. I'm using fetch, how can I get the file's path using <input type="file"/>?

I've tried

const input = document.querySelector('input[type="file"]');
const data = new FormData();
data.append('file', input.files[0]);
console.log(data)

but there's no file path.



via Jenny Mok

Node script does not exit properly when it's running as cron job

I am trying to use this node script (FP2DOM.js).

If I run manually from terminal, the script is always terminating correctly.

If I run as a cron job:

*/15 * * * * node /root/domoticz-flower-power/FP2DOM.js a0143d7d9338 >/root/domoticz-flower-power.log

The script does not exit when it's done, and another cron job creates new instance. So every cron job creates new node instance.

The node script contains process.exit(0);, so I don't really understand why it's not exiting correctly, when running as a cron job.

One workaround is to:

*/15 * * * * killall node; node /root/domoticz-flower-power/FP2DOM.js a0143d7d9338 >/root/domoticz-flower-power.log

...but in this case I can't run another node scripts, for obvious reasons.



via Adrian

Is it good practice to run a backend and a fronted server with express in NodeJS?

i am currently working on a simple NodeJS/AngularJS application and my current project looks like this:

/frontend/index.html <-- AngularJS home page
/frontend/js/app.js  <-- AngularJS app.js
/backend/package.json<-- NodeJS package.json
/backend/index.js    <-- NodeJS script

and I am only serving the backend API with my NodeJS express server while I open the frontend part directly (file://.../frontend/index.html) in my browser.

Is it good practice to serve both the "Backend API" and the frontend with NodeJS (e.g. with two express instances running) and how would I organize the code?



via daG