Custom routes to fetch json from SQL


Is there any ways or instructions on how to extend the Ghost blog to set up a custom route? I want to extend my Ghost blog to let users fetch MySQL data in my server in json. For example accessing /data?month=3 returns me the relevant data from the MySQL query.

The routing is very simple and just parses the URL, run the relevant MySQL query, and return the response as json.

Is there any way to implement it? I have a bit of rudimentary node.js experience but which files should I modify under the core/server?

It seems that I can implement it in core/server/web/site/app.js. First, locate the SiteRouter(req, res, next) function, and write whatever I want to do within the function, and if the URL path does not belong to my custom routes, return the router(req, res, next); which is implemented at default.

I’m not sure if this is the best way or may trigger potential bugs, but at least on my very simple custom routing, this works fine. I’m willing to hear any suggestions, though.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.