Hi, i'm new in node and express.I have a route handler that may call different functions depending on some request parameters, and I would like to know what's the best way to deal with errors inside the functions in order to pass errors to the error handling middleware.Consider something like this:router.get('/error/:error_id', (req, res, next) => { my_function(); } function my_function(){ // do something async, like readfile var f = fs.readFile("blablabla", function (err, data) { // would want to deal with the error }); } If an error occurs during fs.readFile, how do I pass the error to next to forward it to the error middleware?In case the function didn't call any async I/O operation, a simple try/catch in the route handler would be ok (i suppose), like this:router.get('/error/:error_id', (req, res, next) => { try{ my_function(); } catch(e){ next(e); }; } function my_function(){ // do stuff var f = fs.readFileSync("blablabla"); // possibly throws an error } Cheers
Submitted April 07, 2017 at 02:29PM by honestserpent
Friday, 7 April 2017
Legacy debugger in Node.js will be removed in 8.0.0 *without* a normal deprecation cycle
https://twitter.com/jasnell/status/849743288907079680
Submitted April 07, 2017 at 12:57PM by ffalci
Submitted April 07, 2017 at 12:57PM by ffalci
Learn NodeJS With Me (Part 4) - ECMAScript5 - Getters And Setters
https://www.youtube.com/attribution_link?a=ZWcUHDGtZJk&u=%2Fwatch%3Fv%3D7k2tOB0hnG8%26feature%3Dshare
Submitted April 07, 2017 at 01:04PM by grouseydaryl
Submitted April 07, 2017 at 01:04PM by grouseydaryl
Node.js Weekly Update - 7 April, 2017
http://ift.tt/2nKuztf
Submitted April 07, 2017 at 09:36AM by hfeeri
Submitted April 07, 2017 at 09:36AM by hfeeri
Learn NodeJS With Me (Part 2) - What is Node JS?
https://www.youtube.com/attribution_link?a=pme8vHxRS68&u=%2Fwatch%3Fv%3DLCu3gPQuPvk%26feature%3Dshare
Submitted April 07, 2017 at 08:55AM by grouseydaryl
Submitted April 07, 2017 at 08:55AM by grouseydaryl
CloudBoost: Open Source Serverless platform written in NodeJS.
http://ift.tt/1QaWJed
Submitted April 07, 2017 at 09:09AM by nawazdhandala
Submitted April 07, 2017 at 09:09AM by nawazdhandala
Thursday, 6 April 2017
error sending parameters from routes file to node js script
Hey guys,I am new to node js but i am trying to develop a web app to scrape data from a site as personal project. I am currently having trouble trying to send over the params collected from a form in the node web app to a node js script.Project structure (express-handlebars): routes folder(page.js) and public folder(javascripts/scrape.js)page.jsvar express = require('express'); var router = express.Router(); var scrapeCall = require('./scrape.js'); var obj; var nformfield; /* GET users listing. */ router.get('/:fname', function(req, res, next) { nformfield = req.params.fname; // param i want to pass to scrape script scrapeCall.getData(); res.render('submit', { title: 'Project Name'}); }); obj = {nfield: nformfield}; // setting it to global json to use it in scrape.js module.exports.obj = obj; module.exports = router; scrape.jsvar express = require('express'); var path = require('path'); var request = require('request'); var cheerio = require('cheerio'); ..... var pagejs = require('../../routes/page'); //import the route file function getData(){ var url = "www.URL.com"; request(url, function(err, resp, body){ var $ = cheerio.load(body); // if(err){ console.log(err); }else{ console.log("in function") console.log(pagejs) // everytime the values come over as undefined but they printout fine in page.js } }); app.listen(port); console.log("server running on port "+ port); } Console output:in function { obj: { nfield: undefined } } Processed Data .. Do you guys have any idea why i could be getting this undefined value?
Submitted April 07, 2017 at 05:38AM by tht_chico
Submitted April 07, 2017 at 05:38AM by tht_chico
Subscribe to:
Posts (Atom)