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 express app, it doesn't seem. So I see in the nodebb-custom-pages plugin bunny parses the url, which is ok I guess, but I am still limited it seems I can only res.render() I cant use methods like res.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 do res.json



  • 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?


  • Community Rep

    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!


  • Admin

    Also, you'll want to add new routes to params.router, which is a special router just for plugins, and doesn't interfere with existing routes :smile:


Log in to reply
 

Looks like your connection to NodeBB was lost, please wait while we try to reconnect.