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?
Asked
Active
Viewed 1,677 times
2
-
Not without external tools. [NodeJS](http://nodejs.org/) has this functionality, and you can import Sailsjs by checking out [this answer](http://stackoverflow.com/questions/5797852/in-node-js-how-do-i-include-functions-from-my-other-files). – Adam Apr 14 '14 at 17:02
1 Answers
4
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
.

sgress454
- 24,870
- 4
- 74
- 92
-
The link is broken. Probably referring to this: https://github.com/balderdashy/sails-docs/blob/master/concepts/Services/Services.md – justin May 16 '15 at 16:19