I'm trying to write a controller in express
that takes some markdown and returns some HTML for forum posts.
I get the expected behaviour at the command line:
> var md = require('node-markdown').Markdown;
undefined
> md("\n\ndon't mind me\n====")
'<h1>don\'t mind me</h1>'
But then if I receive a HTTP POST
request which according to Chrome has the following form data
raw:\n\ndon't mind me\n====
I receive this response:
<p>\n\ndon't mind me\n====</p>
It's not correctly identifying the H1
tag.
Here's the code for parsing the markdown:
var md = require('node-markdown').Markdown;
var parseMarkdown = (function () {
return function (raw_md) {
return md(raw_md);
}
}());
Here's my controller code:
app.post('/render_markdown', function (req, res) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Content-Type', 'application/json');
var raw = req.body['raw'];
res.send({ html: parseMarkdown(raw)});
});
Anyone got a clue what might be going on here? I've tried fiddling with escaping and unescaping when passing the input into the Markdown function and it didn't seem to help.
I think you need to send a string containing the json, instead of the actual object in the response. Try this:
res.send(JSON.stringify({ html: parseMarkdown(raw)});