How add another route with another .tpl file


  • GNU/Linux

    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?


  • GNU/Linux

    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);

  • GNU/Linux

    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?


  • GNU/Linux

    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();
    };
    

  • GNU/Linux

    @frissdiegurke said:

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

    @frissdiegurke thanks!!!! It workss fine!


  • Admin

    Thanks for providing much needed assistance @frissdiegurke 😄

    Welcome to the land of plugin creation @Doppy !!




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