简体   繁体   中英

Can I run sailsjs controller methods from the command line?

I am writing an application with Sailsjs which includes a scraper. Currently I'm calling the scraper functions from the browser using the default Routes but I think it would be better if I can privately call the needed methods from the terminal. Is it possible to do this?

You can use the Sails console to access controller methods, but you'd have to either provide fake req and res objects as arguments, or create controller functions which didn't require req and res , which isn't good practice. A better approach would be to move the scraping code into a service , which is a library that Sails makes globally available for you. For example, if you had a file /api/services/ScraperService.js with:

module.exports = {

   scrape: function (url, fileToSaveResultsTo) {
      // do scraping and save to file
   }

}

then you could call the service from within a controller or in the Sails console with:

ScraperService.scrape("http://google.com", "results.txt");

Start the console in your terminal with sails console .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM