Join the Stack Overflow Community
Stack Overflow is a community of 6.4 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

I'm trying to read directory that has some image files from Node.js and utilize the data in angularjs but I'm not sure my readdir is correctly working.

here's my server.js code

'use strict';
const express = require('express');
const fs = require('fs');
const app = express();
const http = require('http');
const path = require('path');
const port = process.env.PORT || 8000;
const server = http.createServer(app);

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

app.get('/api/loadAll', function(req, res) {
  fs.readdir(path.join(__dirname, '../client/assets'), function(err, imgs) {
    if (err) { throw err; }
    console.log('imgs', imgs);
    res.send(imgs);
  });
});

server.listen(port, function() {
  console.log('Listening on port ' + port);
});

and here's my angularjs factory which receives from '/api/loadAll'

'use strict';
angular.module('app.service', [])
.factory('Images', function($http) {
  var loadAll = function() {
    return $http({
      method: 'GET',
      url: '/api/loadAll'
    })
    .then(function(res) {
      console.log('res', res);
      return res;
    });
  }

  return {
    loadAll: loadAll
  };
});

hope these blocks of code are enough information to see what I'm doing wrong.

Thanks in advanced!!

share|improve this question
    
Quick question before I post my answer: are you trying to send a file for the client to download on the machine, or are you just trying to get images to display on your site? – joh04667 Sep 13 at 14:48
    
I want to simply display on my site. – ckim16 Sep 13 at 14:54
    
Oh okay, that's much simpler. I'll write an answer quick – joh04667 Sep 13 at 14:54
    
Thank you!! @joh04667 – ckim16 Sep 13 at 15:01
    
One more question, because as I look at this I don't see anything wrong: what is the error you are getting? I am assuming you're trying to get an array of filenames to send to the frontend and have Angular load load them. Are the images in this folder going to be changed dynamically? – joh04667 Sep 13 at 15:28

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.