Using Onsen, how to write server side code in NodeJs that will render Jade files as HTML in a splitter

rgins16

In my Onsen app I have the following splitter. I am using Jade, and rendering all the other pages from the list items in html (despite the fact that they are in separate jade files) by including the files at the bottom of the page, as shown below:

body(ng-controller='...')
    ons-splitter(var='mySplitter')
      ons-splitter-side(var='menu' side='left' width='220px' collapse swipeable)
        ons-page
          ons-list
            ons-list-item(ng-click="root.load('home.jade')", tappable='')
            | Home
          ons-list-item(ng-click="root.load('search.jade')", tappable='')
            | Search
            ... more list items

    ons-template(id='home.jade')
      ons-page(ng-controller='...')
        ons-toolbar
        .left
          ons-toolbar-button(ng-click='mySplitter.left.open()')
            ons-icon(icon='md-menu')
        .center
          | My App

        //- google maps stuff
        ons-input#pac-input.controls(type='text', placeholder='Search Box')
        div#map.col-md-12

        ons-bottom-toolbar
          .center
          | MyApp

    include search.jade

I believe this is a dirty shortcut, and will load the contents of search.jade (as well as every other file I include) before the user even clicks the item in the splitter.

I do not want this functionality. I would like to instead have server code in NodeJs that renders the jade files in html when they are ready to be displayed to the user. Something like this:

jade.renderFile('search.jade')

This angular code is currently how I am loading the page from the item in the splitter:

mySplitter.content.load(page)
  .then(function() {
  $scope.pop = page;
  mySplitter.left.close();
});

However I am very confused about how to write this in a node route. Do I just abandon the splitter function in angular?

Can anyone help clarify this for me and show me a clear example of how to write the node route to render the jade files as html each time they are loaded?

Please see solution 1 of the selected answer from this stack overflow post for a reference of what exactly I am trying to do: stack overflow post

I am currently using solution 2 from that post.

I believe this is the relevant code in server.js:

app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');

However when I change jade to html, I receive the error: Error: Cannot find module 'html'

All my front-end files in the views folder have .jade extensions and are written in jade.

Update Here is how I am serving index.jade (which is in the views folder) in a file called index.js:

module.exports = function(app){

  /* Get home page. */
  app.get('/', function(req, res, next) {
    res.render('index', { title: 'My App' });
  });
}

This was my old route NodeJS route which is no longer being used because of the splitter:

    // get user search page
    app.get('/user/search', function(req, res, next) {
        return res.render('searchForTrainer');
    });
Ilia Yatchev

Hmm. Since your code seems relatively small I would guess that what it does may be just serving all your files from views and actually "rendering" them. So probably you are just failing to access them properly later on. Maybe you have a url like /search.html or /search (instead of /search.jade). Could you try to confirm whether you can access such a url?

Also is your index.jade file served in some other way like startingPoint: 'index.jade' or something similar or is it also located in the views folder?

Basically as long as your index file has the same treatment as your other views then everything should be fine.

Update: With what you just provided we can see the way in which you are serving your index.

app.get('/', function(req, res, next) {
    res.render('index', { title: 'Fitness App' });
});

The equivalent of that is exactly the same as what you said you had before:

app.get('/user/search', function(req, res, next) {
    return res.render('searchForTrainer');
});

Here res.render is what converts your jade into html and then returns it to the client. Since the splitter is expecting html that means you shouldn't have made changes to the server when you started using it.

Here is how the process looks like:

       Client          |        HTTP         |      Server
                       |                     |
 content.load('page')  →     GET /page       ↘ 
                       |                     | res.render('page') // convert jade to html
   html is loaded      ← 200 OK html content ↙
 in splitter.content   |                     |

TL;DR - if you use your old route everything should be fine. Just remember to change the page url in the splitter from search.jade to /user/search (or whatever the url for will be).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to programmatically render dustjs template server side in nodejs

From Dev

How to pass data from jade to mongodb using NodeJS as the server language

From Dev

Writing server side code using nodejs on parse server app

From Dev

Using multiple html files in onsen UI

From Dev

Write javascript code that works with client side javascript and server side NodeJs modules

From Dev

Onsen-UI splitter-side always closed

From Dev

Onsen UI ons-splitter-side events don't work

From Dev

How to render a jade block(section) using links?

From Dev

how to render JavaScript using pug/jade

From Dev

render html file using nodejs

From Dev

Render React JS server side using .net

From Dev

Write plain HTML in Jade

From Dev

How to include html code into Jade file?

From Dev

How to code HTML with jade and webpack with hot reload

From Dev

Using HTML Templates at server side

From Dev

How to implement 'server side' Stripe code into Android App using SQLite

From Dev

Server-side push using NodeJS

From Dev

Jade(Pug) Html render output

From Dev

How to indent HTML output using GULP JADE

From Dev

How to send a lot of files from server to client using express (NodeJS)

From Dev

How to store datetime on server side in nodejs?

From Dev

How to write text to a server-side file

From Dev

JQuery & jade read render data from Nodejs

From Dev

React server-side render or static index.html?

From Dev

How to debug nodejs code in render process of electron?

From Dev

NodeJS, how to render static HTML with Express 4?

From Dev

How to render a HTML page form NodeJs API?

From Dev

Using server-side-render only for initial page load

From Dev

Using server-side-render only for initial page load

Related Related

HotTag

Archive