Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> Node export module eksportuje nie to co chcę
amii
post
Post #1





Grupa: Zarejestrowani
Postów: 728
Pomógł: 76
Dołączył: 12.06.2009

Ostrzeżenie: (0%)
-----


Mam taki plik, który ma korzystać z modułu:
[JAVASCRIPT] pobierz, plaintext
  1. var mymodule = require('./filtered2Module.js');
  2.  
  3. var dir = process.argv[2]; //katalog w ktorym wyszukujemy pliki
  4. var ext = process.argv[3]; //koncowka pliku w/g ktorej filtrujemy wyszukiwanie
  5.  
  6. mymodule(dir, ext, function(err, list){
  7. if (err) {
  8. throw err;
  9. }
  10. else {
  11. list.forEach(function(el){
  12. console.log(el);
  13. }
  14. )
  15. }
  16. });
  17.  
  18. console.log(JSON.stringify(mymodule.calc()));
[JAVASCRIPT] pobierz, plaintext


A to moduł:
[JAVASCRIPT] pobierz, plaintext
  1. var fs = require('fs');
  2. var path = require('path');
  3.  
  4. module.exports = function(dir, ext, callback) {
  5. return {
  6. calc: function() {
  7. var buf = fs.readdir(dir, function (err, data) {
  8. if (err) {
  9. return callback(err); // early return
  10. }
  11. // ... no error, continue doing cool things with `data`
  12. var list = [];
  13. for (var i=0, len=dir.length; i<len; i++) {
  14. if (path.extname(dir[i]).substring(1) == ext) {
  15. list.push(dir[i]);
  16. }
  17. }
  18. // all went well, call callback with `null` for the error argument
  19. callback(null, list);
  20. });
  21. }
  22. };
  23. }
[JAVASCRIPT] pobierz, plaintext


Jak wywołuję pierwszy plik to dostaję zwrotkę, że mymodule jest obiektem. Posiłkuję się dokumentacją node gdzie jest podobny przykład dla modułu square. Po za tym jak dla mnie to wygląda to tak, że zwracana jest funkcja, która zwraca obiekt i całość jest eksportowana.
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 24.08.2025 - 09:44