fs.readFileSync on server side [SOLVED]


#1

I am stuck trying to read a file on the server. When I connect to meteor shell I have no problem using fs.readFileSync(absolutePath) but when I put the same code into my server code, it only prints IncomingMessage { ... } in my server log.

Does anybody know how to read a file on the server with the fs module?


#2

This was some stupid mistake I made where I hit my application with curl -I but didn’t implement the request method HEAD on the server and thus ended up with the request method printed out.

It had nothing to do with fs.readFileSync. Here is my full route

Picker.route('/widget/:widget', function(params, request, response, next) {
  switch(request.method) {
    case 'GET':
      var path = process.cwd().split('/');
      path.pop();
      path.push('web.browser');
      path.push('app');
      path.push('widget');
      path.push(params.widget+'.js');
      var widgetPath = path.join('/');
      console.log(' - get widget '+widgetPath);
      if(fs.existsSync(widgetPath)) {
        var js = fs.readFileSync(widgetPath).toString();
        response.writeHead(200, {
          'Content-Length': js.length,
          'Content-Type': 'application/javascript'
        });
        response.end(js);
      } else {
        response.writeHead(404, {
          'Content-Type': 'html/text'
        });
        response.end("file not found");
      }
    default:
     console.log(request)
  }
})