javascript - Undefined HTTP GET Request Interpreted by Node.js Server -


i having issue node.js application sending http request through ajax call client-side, receiving request server-side, , trying use data sent through request. issue server says request undefined. have absolutely no idea why case doing other similar requests on other pages , work fine.

here error message:

/var/www/html/adminextension/extension/extension/node_modules/find/index.js:35           throw err;                 ^ error: enoent, lstat '/path/to/project/undefined' 

the "undefined" bit supposed rest of path retrieved request.

here relevant app.js code:

var express = require('express'); var memorystore = require('connect').session.memorystore;  var app = express();  app.set('port', process.env.port || 3333); app.set('views', __dirname + '/views'); app.set('view engine', 'ejs'); app.set('view options', {layout: false}); app.use(require('morgan')('dev')); app.use(require('body-parser')({ keepextensions: true})); app.use(require('cookie-parser')()); app.use(require('express-session')({ secret: '12345678910', store: new memorystore({ reapinterval: 600000}) })); app.use(require('method-override')()); app.use(function(req, res, next) {         res.header("access-control-allow-origin", "*");         res.header("access-control-allow-headers", "x-requested-with");         next(); }); app.use(express.static(path.join(__dirname, '/public')));  app.all('*', function (req, res, next) {     res.header("access-control-allow-origin", "*");     res.header('access-control-allow-methods', 'get');     next(); });  // routes browsing directories , listing contents var browse = require('./routes/general/browse'); app.get('/browse/file/info', browse.getfileinformation);  app.listen(app.get("port")); 

server-side browse.js code:

var path = require('path'); var fs = require('fs');  exports.getfileinformation = function(request, response) {     var filepath = "/folder1/folder2/" + request.body.filepath;     console.log('*' + filepath + '*');     checkfileinfo(             filepath,             function(err, fileinfo) {                 if (err)                     console                             .log("an error has occured when trying retrieve file information: "                                     + err);                 else                     response.send(fileinfo);             }); };  function checkfileinfo(filepath, callback) {     fs             .lstat(                     filepath,                     function(err, stats) {                         if (err)                             return callback(err);                         else if (stats && stats.isfile()) {                             var fileinfo = {};                             fileinfo.size = stats.size;                             fileinfo.created = stats.ctime;                             fileinfo.modified = stats.mtime;                             fileinfo.accessed = stats.atime;                              callback(null, fileinfo);                         } else                             return callback("the path provided did not lead accessible file.");                     }); } 

and client-side ajax request:

var obj = {     filepath : "/relative/path/this_is_a_file.txt" };  $.ajax({     type : "get",     url : serverurl + '/browse/file/info',     data : obj,     datatype : 'json',     success : function(filedata) {         // else     } }); 

i appreciate input.

the problem in checkdata function:

function checkdata(data) {     console.log(json.stringify(request.body));     return data; } 

it receives data , use request object - it's not accessible in context.

you can add parameter called request function:

function checkdata(data, request) {     console.log(json.stringify(request.body));     return data; } 

and send request object:

checkdata(request.body.filepath, request) 

update

after you've updated question original problem appeared.

i geuss undefined problem got there because you're accessing request body - request has body parameters if it's post request (get requests has url , headers).

try changing client side ajax call type : "get" type : "post" , update nodejs:

app.get('/browse/file/info', browse.getfileinformation); 

to

app.post('/browse/file/info', browse.getfileinformation); 

i recommend file path __dirname + '/path' avoid problems.


Comments

Popular posts from this blog

how to proxy from https to http with lighttpd -

android - Automated my builds -

python - Flask migration error -