Hexo: Render a part of hexo with existing ExpressJS server and EJS

Created on 7 Jan 2019  ·  2Comments  ·  Source: hexojs/hexo

Good Day!

Basically I wanted to ask, whether it's possible to render just a part of a hexo template with my existing ExpressJS server and EJS.

Say I have a route /blog

app.get("/blog", (req, res) => {
    res.render("blog", {
        "route": req.path,
    });
});

and an EJS template blog.ejs.
Now I want to include a part of hexo in this blog.ejs file like so:

<%- include("global/header"); -%>
    <div class="main-content">
        <% /* I want to include the blog part of hexo without header/footer here */ %>                            
    </div>
<%- include("global/footer"); -%>

Is that possible?

Most helpful comment

Hexo Rendering API is what you need.

Before calling res.render, use the Hexo rendering API to render the blog and then pass the result to locals parameter.

To exclude header or footer, customizing templates in your Hexo theme can achieve this.
Check out this page for more info about Hexo templates.


I think there is another way worth trying to render the Hexo blog under route /blog.

By Hexo router API, you can make use of the goodness of NodeJS Stream API like the following example code.

Since hexo.route.get() returns a Readable, you can pipe it to the express res object, which is a Writable.
pipe method will end the response for you by default.

app.get("/blog", (req, res) => {
    hexo.route.get(req.path).pipe(res);
});

After delegating the rendering job to Hexo, include your blog.ejs as a part of customized templates in your Hexo theme.

It just another information for you. Not testing it myself, but it seems feasible and pretty enough!

All 2 comments

Hexo Rendering API is what you need.

Before calling res.render, use the Hexo rendering API to render the blog and then pass the result to locals parameter.

To exclude header or footer, customizing templates in your Hexo theme can achieve this.
Check out this page for more info about Hexo templates.


I think there is another way worth trying to render the Hexo blog under route /blog.

By Hexo router API, you can make use of the goodness of NodeJS Stream API like the following example code.

Since hexo.route.get() returns a Readable, you can pipe it to the express res object, which is a Writable.
pipe method will end the response for you by default.

app.get("/blog", (req, res) => {
    hexo.route.get(req.path).pipe(res);
});

After delegating the rendering job to Hexo, include your blog.ejs as a part of customized templates in your Hexo theme.

It just another information for you. Not testing it myself, but it seems feasible and pretty enough!

Thank you very much! :)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

scaljeri picture scaljeri  ·  44Comments

SukkaW picture SukkaW  ·  22Comments

dsaltares picture dsaltares  ·  46Comments

JackieLuo picture JackieLuo  ·  45Comments

tomap picture tomap  ·  21Comments