• Home
  • Categories
  • Recent
  • Popular
  • Top
  • Tags
  • Users
  • Groups
  • Documentation
    • Home
    • Read API
    • Write API
    • Plugin Development
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
v3.5.2 Latest
Buy Hosting

How to add my own API?

Scheduled Pinned Locked Moved Technical Support
5 Posts 3 Posters 522 Views
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Ape
    wrote on last edited by
    #1

    I want to add my own custom route that can take POST requests and write my own logic to provide a dynamic response.

    I tried nodebb-plugin-custom-pages, but seems like that only does a GET request and only way to add content there is by ACP->/extend/widgets.

    Im not advanced enough to write my own plug ins , is there a simpler way to achieve what I need?

    julianJ 1 Reply Last reply
    0
  • julianJ Offline
    julianJ Offline
    julian GNU/Linux
    replied to Ape on last edited by
    #2

    @ape nope, unfortunately not. You'll have to create a plugin and attach a listener to static:api.routes.

    Check out the quickstart plugin, it has example logic for this hook.

    A 1 Reply Last reply
    1
  • A Offline
    A Offline
    Ape
    replied to julian on last edited by
    #3

    @julian Thanks for the reference.

    I loaded the Quickstart plugin and followed the example.
    Added my route but I’m still getting “not found” when I go to http://localhost:4567/test321.

    plugin.addRoutes = async ({ router, middleware, helpers }) => {
    	function render(req, res, next) {
    	  helpers.formatApiResponse(200, res);
    	  //  res.send('Test') // tried this also
    	};
    	router.get('/test321', render);
    	router.get('/quickstart/:param1', middleware.authenticate, (req, res) => {
    		helpers.formatApiResponse(200, res, {
    			foobar: req.params.param1,
    		});
    	});
    };
    
    dogsD julianJ 2 Replies Last reply
    0
  • dogsD Offline
    dogsD Offline
    dogs
    replied to Ape on last edited by
    #4

    @ape Take a look at my yesterday's topic.

    Had similar problems, maybe the topic helps you.

    Custom route in plugin drops 404 error on not defined client script

    Hello! I set up a custom route via own plugin: plugin.init = async function (params, callback) { var router = params.router; var middleware = params.middl...

    favicon

    NodeBB Community (community.nodebb.org)

    routeHelpers.setupPageRoute(router, '/discord/topic/', middleware, [(req, res, next) => {
    		winston.info(`[plugins/discord-topic] In middleware. This argument can be either a single middleware or an array of middlewares`);
    		setImmediate(next);
    	}], (req, res) => {
    		winston.info(`[plugins/discord-topic] Navigated to ${nconf.get('relative_path')}/discord-topic`);
    		render(req, res);	// replace this with res.render('templateName');
    	});
    
    1 Reply Last reply
    1
  • julianJ Offline
    julianJ Offline
    julian GNU/Linux
    replied to Ape on last edited by julian
    #5

    @ape if you're using that hook, the routes are mounted to /api/v3, so the definition you provided would end up being accessible at /api/v3/test123

    1 Reply Last reply
    2

Copyright © 2023 NodeBB | Contributors
  • Login

  • Don't have an account? Register

  • Login or register to search.
Powered by NodeBB Contributors
  • First post
    Last post
0
  • Home
  • Categories
  • Recent
  • Popular
  • Top
  • Tags
  • Users
  • Groups
  • Documentation
    • Home
    • Read API
    • Write API
    • Plugin Development