Templates.js html server rendering
-
Usecase
- User make an API request on url, eg. http://localhost:4567/api/test?message=world.
- Server render the template with the message "world" and send back the response, eg.
Template hello.tpl:
<html> <body> <div>hello {message}</div> <body> <html>
The code in the controller is
res.render('user/hello.tpl', req.query);
. The response I got back is a json instead of a html page:{ "message": "world", "loggedIn": true, "relative_path": "", "template": { "name": "user/hello.tpl", "user/hello.tpl": true }, "url": "/hello", "bodyClass": "page-hello" }
What have I miss? Please advise, thanks!
-
The NodeBB middleware automatically sets the response type to JSON for any route beginning with
api
Set the begining of the route to anything other than
api
and it should work. -
Alternatively, I think setting
res.locals.isAPI = false
before you render would also work.
-
@Bruce-Lee first, remove the
.tpl
from the route.
Copyright © 2024 NodeBB | Contributors