2

How would I modify the text in a firepad editor on my webpage using Node? What I'm trying to do is have the user upload a file, then read the contents of the file, and replace the firepad contents with the uploaded file's contents.

I see that there's a firepad npm module https://www.npmjs.com/package/firepad, but it doesn't say how to use it with node. I currently have it setup in my html and using a normal public js file in my node project.

So I was thinking that to update the text, I could either send a post request with the text, and my client side js would receive it and then update the firepad since it has access to the firepad variable there.

Or I could use the npm module, but I'm not sure if it's a full module.

rasen58
  • 4,672
  • 8
  • 39
  • 74

2 Answers2

2

Take a look at the Firepad Headless docs. Here's an example:

var Firepad  = require('firepad')
var firepadRef = ... // create firebase database reference to your firepad data.
var headless = new Firepad.Headless(firepadRef)

headless.setText('Welcome to Firepad!', function(err, committed) {
  // *err*       will be set if there was a catastrophic failure
  // *committed* will be true on success, or false if there was a history
  //               conflict writing to the pad's history.
});
Michael Lehenbauer
  • 16,229
  • 1
  • 57
  • 59
  • So actually, when I include just that first line `var Firepad = require('firepad')`, I get an error `ReferenceError: window is not defined at c:\Users\user\git\eebooks\node_modules\firepad\dist\firepad.min.js:13:179`. I think it's because I'm running this on Node which is server-side and doesn't have access to the browser's window object. So what should I do? – rasen58 Jun 20 '16 at 00:25
  • Ack! Hrm. Looks like something got broken. Can you try using firepad 1.3.0 for now? I've opened an issue to look into fixing 1.4.0: https://github.com/firebase/firepad/issues/251 Thanks for the report. – Michael Lehenbauer Jun 20 '16 at 15:19
  • I was able to get it to work by replacing all instances of `window` with `global` – rasen58 Jun 20 '16 at 17:05
  • I'm using webpack and get this warning: `Critical dependencies: 2463:61-68 require function is used in a way in which dependencies cannot be statically extracted @ ./~/firepad/dist/firepad.js 2463:61-68` – Cameron Oct 11 '16 at 01:13
0

A workaround till the available npm version (1.4.0) gets the patch is to define window:

   global.window={};
   const Firepad = require('firepad');
David I. Samudio
  • 2,364
  • 18
  • 21