Custom api router?
-
I want to build my own api, I have a custom composer and a custom post type that consists of only an image and title, it acts as a feed.
Inside my theme I want to build a custom api router, for now I have figured out how to add data to the database, but I am limited in the router.
var theme = {}; var db = module.parent.require('./database'); var Categories = module.parent.require("./categories"); theme.init = function(params, callback) { var app = params.app; var router = params.router; app.get('/api/test/:id', function(res, req, next) { console.log(req.params); res.json(req) // db.set('test', req.params.id, function() { // res.json({working:true}); // }); }); callback(null); };
The
app
in this case is not the typical expressapp
, it doesn't seem. So I see in thenodebb-custom-pages
plugin bunny parses the url, which is ok I guess, but I am still limited it seems I can onlyres.render()
I cant use methods likeres.json()
.Any advice?
-
@Michael-Joseph-Aubry I found to get the is I can use
req.req.params
, now I am going to figure out how to dores.json
-
Michael Joseph Aubryreplied to Michael Joseph Aubry on last edited by Michael Joseph Aubry
Figured it out
res.res.json({"test": true})
Edit:
@psychobunny why dont you do
req.req.params.id
inside your custom pages plugin instead of parsing the url? -
Looks like you just have the parameters reversed, it should be
function(req, res, next)
res.json should work fine after you fix that part.
-
Oh damn lol I messed that up. Ok cool thanks!