How to render a PUG XML page with Express

Viewed 16

I am trying to set up RSS Feed for my blog using PUG as the XML template and Express. The answer to this question gave me the idea and I used it as my reference.

The problem: The RSS file is not rendering properly (see picture). The text is showing as a large, uninterrupted paragraph without the conventional XML tags of all RSS Feeds. These show only after that long paragraph.

These were my steps:

I created the rss.pug file using the conventional XML/RSS structure and added it to my views folder.

rss.pug :

doctype xml
rss(version="2.0", xmlns:content="http://purl.org/rss/1.0/modules/content/", xmlns:atom='http://www.w3.org/2005/Atom')
  channel
    title devenir escritor
    description Los Ășltimos posts del blog devenir escritor. 
    language es 
    copyright Copyright 2022 Martin Chirino
    -const url= 'https://devenir-escritor.com/'
    link= url
    lastBuildDate= posts[0].publishedAt 
    each post, i in posts
      item
        title= post.title
        -const postUrl = `${url}post/${post.slug}`
        guid(isPermaLink='true')= postUrl
        pubDate= post.publishedAt
        description= post.summary
        each tag in post.tags
          category= tag
(more code...)

Then I used the same I am using for all my views:

In app.js:

app.use('/', viewRouter);

In viewsRoutesjs:

router.get('/rss', viewController.getRSS);

And in my viewController function, I first search the database for the last 10 Posts and render the xml pug page with the data.

exports.getRSS = async function ....
... (search database)

res.type('xml');
  res.status(200).render('rss', {
    title: 'RSS',
    posts,
  });
});

It all works well until it displays the page. The XML file (with all the correct data fetched) is rendered as one blob of text inside the body of an HTML file. And it looks terrible.

XML inside Body

I would like it to look like a "normal" XML/RSS feed: New York Times - Business Feed

I think that my problem has to do with the rendering of the file. It should't show an HTML file at all, just an XML one. Or with PUG that's not possible?

0 Answers
Related