Last active
November 15, 2018 13:18
-
-
Save narirou/a186e62d5c3891e2ff44 to your computer and use it in GitHub Desktop.
express.js + pjax workflow
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
module.exports = function() { | |
return function pjax( req, res, next ) { | |
if( req.header( 'X-PJAX' ) ) { | |
req.pjax = true; | |
res.locals.pjax = true; | |
} | |
next(); | |
}; | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extends layout | |
block contents | |
p another page! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extends layout | |
block contents | |
p index page! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-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' ); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you!!!