Writing Jade to a file

179 views
Skip to first unread message

Abraham itule

unread,
Sep 13, 2012, 12:38:14 PM9/13/12
to jad...@googlegroups.com
Here is What am trying to do.
I made a simple docs sharing application. So u can write a new document and share that document by giving its unique URL to someone. E.g. lets say my website is, www.quickdocs.com, if you make a new document, the app will provide u a new unique URL e.g. www.quickdocs.com/j2341kk3n5k5asd32/ and u can then share that URL to someone.

Having said that, what am trying to know if there's a way to write the HTML that jade outputs to a file 

Jan Buschtöns

unread,
Sep 13, 2012, 2:53:22 PM9/13/12
to jad...@googlegroups.com
I'm sure you know how to use fs. No need for explanation here.
Here you can see how to use Jade without express. When giving a filename in the options your users will be able to use extend and include, which could be a security thread.

2012/9/13 Abraham itule <wand...@gmail.com>

Abraham itule

unread,
Sep 13, 2012, 3:23:41 PM9/13/12
to jad...@googlegroups.com
Its easy to do it without jade. coz am received POST data and just doing
fs.createWriteStream( path )
- Then redirecting the user to the path.

so in terms of code demo, with jade 
var jade = require('jade');

var fn = jade.compile( // Write some Jade Code Here, { filename: 'public/j234kl5a2j6s3g4/'});fn( req.params.textArea ); // textArea is the name of the data am sending.
Would that be correct???

- And in the place to write jade Code, since the string is like kinda long, roughly 200 lines of code, it would look ugly, do write code there. Other Cleaner options?? Other than making a module and doing a require with a toString which is similarly ugly.

aaaaargh, i might aswell just write raw html

silvinci

unread,
Sep 13, 2012, 3:37:32 PM9/13/12
to jad...@googlegroups.com
What exactly are you storing? A jade file or a (json) file that stores the given data by the user?

Why don't you just let express handle the jade parsing staff?

Abraham itule

unread,
Sep 13, 2012, 3:45:23 PM9/13/12
to jad...@googlegroups.com
If u read at the beginning of how my app works, it allows a user to share docs with a unique URL. Hence the reason i have to constantly make new html files.
So to answer your question, am not storing json, but am taking the POST data (which is whatever the user has written) and making a new html file with that data inside. and serving it.

My app certainly works already (using raw html), but since am trying to learn Jade, am trying to change my app to work with jade instead. But too much effort, i give up & stick to just raw html. Thanks for ur help.

Jan Buschtöns

unread,
Sep 13, 2012, 3:49:00 PM9/13/12
to jad...@googlegroups.com
Now that I understand you I can help you. 

Generate the jade files and save them. Then just let express do the work.

app.get("/public/:id", function(req, res) {
    res.render("/public/" + req.params.id);
});
360.gif

Abraham itule

unread,
Sep 13, 2012, 3:56:42 PM9/13/12
to jad...@googlegroups.com
That wouldn't work. 
Note: In the HTML, i've already got the header info (logo's & navigation etc), the footer is also sorted. but on the body, i need to append the received POST data. So ideally WITH RAW HTML it should be something like this

app.post("/", function(req, res) {
   var stream = fs.createWriteStream( path )
   stream.write( 
header + req.body.textArea + footer  )
   res.redirect( path )
});

I want to do that with jade.

Abraham itule

unread,
Sep 13, 2012, 4:00:22 PM9/13/12
to jad...@googlegroups.com
so with jade then, lets say i have layout.jade as the base template.

the newly made file should include header and include footer and in between, using locals { postData: req.body.textArea } i should be able to put my data there too.

silvinci

unread,
Sep 13, 2012, 4:14:04 PM9/13/12
to jad...@googlegroups.com
So you're not storing jade files. You're storing data given by the user. And such data preferably shouldn't be saved as a file. You would be better of saving it in a database, like mongoDB, Redis, or some SQL stuff.
You should create a layout which expects a variable (postData) and add its content to a <div> or what not.

app.get("/paste/:id", function(req, res) {
    res.render("/views/paste.jade", { content: yourDatabase.get(req.params.id) });
});

Abraham itule

unread,
Sep 13, 2012, 4:21:46 PM9/13/12
to jad...@googlegroups.com
Aww ma bad, I didn't see this this line: "Generate the jade files and save them. Then just let express do the work."

Which brings us back again, how do i can i generate jade files on the fly and save them? Do i do this?

var jade = require('jade');

var fn = jade.compile(// Write some Jade Code Here, { filename: 'public/j234kl5g4/'});
fn( req.params.textArea ); // textArea is the name of the data am receiving.

And if i do that, is there a cleaner way of writing the jade code, coz if am going to write a string of jade which is like 200 lines of code, its going to look ugly.

Jan Buschtöns

unread,
Sep 13, 2012, 4:32:48 PM9/13/12
to jad...@googlegroups.com
... Using jade only makes sense when you're going to regenerate the files each request via express and get the data from a database as shown above.
Otherwise you should compile static html files and put them in a static directory so express doesn't have to do anything when processing a request.

2012/9/13 Abraham itule <wand...@gmail.com>

Abraham itule

unread,
Sep 13, 2012, 4:35:23 PM9/13/12
to jad...@googlegroups.com
Thanks a lot for you help.

silvinci

unread,
Sep 13, 2012, 4:35:54 PM9/13/12
to jad...@googlegroups.com
You're welcome
Reply all
Reply to author
Forward
0 new messages