0

I am sending filename to server and trying to get some sort of response to make sure api calls are working but its always returing 404 even though path is correct, I am not sure where i am making mistake , Any idea ?

angularjsFactory.js

getFile:function(file_name){
            return $http.get("/file?file_name="+file_name);
        }

server.js

var express = require('express');
var app = express();
var fs = require('fs');
app.use(express.static(__dirname + "/public"));
var readDirectory = require('./readDirectory');

app.get('/file', function(req, res) {
    var path = './ditLogs';
    var fileParam = req.query.file_name;
    console.log(fileParam);
    fs.readdir(path, function(err, items) {
        items.forEach(function(items) {
            if (fileParam === items) {
                res.sendFile(items);
                console.log(items);
            }
        });
    });
});
app.listen(3000, function() {
    console.log('Example app listening on port 3000!');
    //console.log('app is printing data',obj);
    //ditProducer.startProducer();
    setTimeout(function() {
        ditconsumer.start();
    }, 2000);

});
1
  • Did you try adding a forward slash '/' to the end of the url Commented Jun 30, 2016 at 19:05

2 Answers 2

1

Your route on server is not correct comparing to the call from angular's side. You are not using route parameters, but query string.

Try this:

app.get('/file', function (req, res) {
    // get filename using: req.query.file_name
    res.send('FIle data');
 });

Or you can try this on angular and keep the server code:

 $http.get("/file/"+ encodeURI(file_name));
Sign up to request clarification or add additional context in comments.

3 Comments

is there way to pass parameter as i am passing in question ?
i dont know filename is passing as /test.txt , i dont know why
@hussain: coz correct syntax will be: $http.get("/file?file_name="+file_name); if you change node js code to app.get('/file', function (req, res) { // get filename using: req.query.file_name res.send('FIle data'); });
0

Server side code looks incorrect.

app.get('/file', function (req, res) {
    console.log(req.query.file_name);//This will give you filename passed as url param
    res.send('FIle data');
 });

6 Comments

i updated my server code to give you better understanding what i am trying to do
Can you confirm that you are not adding this middle-ware app.get('/file', function (req, res) { // get filename using: req.query.file_name res.send('FIle data'); }); at the last in app.js
Its should be above: ` // catch 404 and forward to error handler app.use(function (req, res, next) { var err = new Error('Not Found'); err.status = 404; next(err); }); ` need to check your app.js
@hussain: all looks well to me dude.. Did you restart the node js server after making the changes? :D Sometime i missed out in early days.. Also just curious to know, what happens if you try hitting by browser?
problem is my if statement is not satisfying because fileParam value is /test.text that is not matching when if(fileParam === file) because file value is without forward / test.txt
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.