Sunday 15 January 2012

javascript - Why do I get 'undefined is not a function'? -


I am trying to return a file as a response to a GET request using express.js A simple was written to handle the file file manager , though I do not have an 'undefined function' error, when I call new FileManager ()

Here, how do I try to:

  // FileManager.js FileManager = function () {}; // Empty Starter FileManager.prototype.handleFileRequest = function (req, res) {var fileId = req.params.id if (file id) {var path = './uploads/events' + fileId; Res.sendfile (path)} other {res.send (404, 'file not found')}}  

Here is the server:

  // Server.js var express = Expected ('Express'); Var File Manager = Requirement ('./FileManager.js'). FileManager; Var app = express (); Var fileman = new file manager (); App.get ('/: id', function (req, res) {console.log (get 'id:' + req.params.id); fileman.handleFileRequest (req, res);}) app.listen ( 'Blabla', 3000);  

But when I run node server.js , I get the following error:

  var fileman = new FileManager (); ^ TypeError: There is no undefined function  

in FileManager.js ... you need Is to export the object.

  module.exports = {FileManager: FileManager}  

server.js

  var file manager = required (' ./ FileManager.js'). FileManager; // This is the FileManager object that was created in FileManager.js  

No comments:

Post a Comment