How add another route with another .tpl file

NodeBB Plugins
  • I need to create another route for show another html result. So I define my new route in my library.js

    plugin.init = function (params, callback) {
    	params.router.post('/user/plugins/answer/home', controllers.renderTest);
    	callback();
    };
    

    In controller I have create my method:

    Controllers.renderTest=function(req,res,next){
    	db.getObject('....', function(err, data) {
    		if (err) { return next(err); }
    		res.render(/user/plugins/test', data);
    	});
    };
    

    My test.tpl page is in /templates/user/plugins and now in my test.tpl page is:

    <p>test</p>
    

    My plugin.json is:

    	"staticDirs": {
    		"static": "./static"
    	},
    	"less": [
    		"static/style.less"
    	],
    	"scripts": [
    		"static/lib/admin.js",
    	],
    	"templates": "static/templates"
    

    So I go at the address http://IPADDRESS/user/plugins/answer/home and I see "/user/plugins/answer/home Not Found Not Found" . Anyone can help me?

  • So I go at the address http://IPADDRESS/user/plugins/answer/home

    You've registered a POST route but trying to access it via browser request I assume.
    POST is for sending data to the server, you need a GET route for the default behavior of your browser.

    // replace this
    params.router.post('/user/plugins/answer/home', controllers.renderTest);
    // with this
    params.router.get('/user/plugins/answer/home', controllers.renderTest);
    
  • thanks frissdiegurke I try your solution and It's work but when I go on the link I see the page and the write, but the background is white, i lost the header and the footer of nodebb. Can you suggest something to do for have the background forum and in the center put my .tpl page?

  • As of the source code of nodebb-plugin-custom-pages (which may fit your needs anyways) I assume you just need to add the buildHeader middleware.

    plugin.init = function (params, callback) {
      params.router.get('/user/plugins/answer/home', params.middleware.buildHeader, controllers.renderTest);
      callback();
    };
    
  • @frissdiegurke said:

    params.router.get('/user/plugins/answer/home', params.middleware.buildHeader, controllers.renderTest);

    @frissdiegurke thanks!!!! It workss fine!

  • Thanks for providing much needed assistance @frissdiegurke 😄

    Welcome to the land of plugin creation @Doppy !!


Suggested Topics