Skip to content

Instantly share code, notes, and snippets.

@narirou
Last active November 15, 2018 13:18
Show Gist options
  • Save narirou/a186e62d5c3891e2ff44 to your computer and use it in GitHub Desktop.
Save narirou/a186e62d5c3891e2ff44 to your computer and use it in GitHub Desktop.
express.js + pjax workflow
var express = require( 'express' ),
bodyParser = require( 'body-parser' ),
pjax = require( './pjax-helper' );
var app = express();
app.set( 'views', __dirname + '/views' );
app.set( 'view engine', 'jade' );
app.use( pjax() );
app.use( bodyParser.urlencoded({ extended: true }) );
app.use( bodyParser.json() );
app.use( express.static( __dirname + '/public' ) );
app.route( '/' ).get( function( req, res ) {
res.render( 'index' );
});
app.route( '/another' ).get( function( req, res ) {
res.render( 'another', { title: 'another page' } );
});
app.listen( 3000 );
'use strict';
module.exports = function() {
return function pjax( req, res, next ) {
if( req.header( 'X-PJAX' ) ) {
req.pjax = true;
res.locals.pjax = true;
}
next();
};
};
extends layout
block contents
p another page!
extends layout
block contents
p index page!
-var SITE_TITLE = title ? title + ' | pjax example' : 'pjax example'
if pjax
title= SITE_TITLE
#contents
block contents
else
doctype html
html
head
meta( charset='UTF-8' )
title= SITE_TITLE
body
ul#links
li
a.pjax( href='/' ) 1
li
a.pjax( href='/another' ) 2
#contents
block contents
scripts( src='//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.1/jquery.min.js' )
scripts( src='//cdnjs.cloudflare.com/ajax/libs/jquery.pjax/1.9.2/jquery.pjax.js' )
scripts.
$( document ).pjax( 'a', '#contents' );
@elkebirmed
Copy link

Thank you!!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment