SHARE
TWEET

Untitled

a guest Oct 10th, 2019 68 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //////
  2. app.js
  3. //////
  4. let hbs = require('express-handlebars');
  5. let logger = require('morgan');
  6. // Example routes
  7. let indexRouter = require('./routes/index');
  8. let apiRouter = require('./routes/api');
  9.  
  10.  
  11. app.use('views', express.static(path.join(__dirname, 'views')));
  12. app.set('view engine', 'hbs');
  13.  
  14. app.engine('hbs', hbs({
  15.     extname: 'hbs',
  16.     defaultView: '',
  17.     layoutsDir: __dirname + '/views',
  18.     partialsDir: __dirname + '/views/partials'
  19. }));
  20. app.use(express.static('public'));
  21. // Logger
  22. app.use(logger('dev'));
  23.  
  24. // Routes example
  25. app.use('/', indexRouter);
  26. app.use('/api', apiRouter);
  27.  
  28. // Bootstrap integration
  29. app.use('/css', express.static(__dirname + '/node_modules/bootstrap/dist/css'));
  30. // insert this in head partial
  31. // head.hbs
  32. <link rel="stylesheet" href="/css/bootstrap.min.css" />
  33. /////////////
  34.  
  35. //layout.hbs
  36. <!DOCTYPE html>
  37. <html>
  38. <head>
  39.     {{> head}}
  40. </head>
  41. <body>
  42. <div id="wrapper">
  43.  
  44.     {{> header}}
  45.     {{{body}}}
  46.     <footer>
  47.         {{> footer}}
  48.     </footer>
  49. </div>
  50. </body>
  51. </html>
  52. ////////////////////////////
  53.  
  54. // index.js route usage example and template rendering
  55. let express = require('express');
  56. let router = express.Router();
  57.  
  58. router.get('/about', (req, res, next) => {
  59.     res.render('about', {layout: 'layout', title: 'About'});  // Here about is your about.hbs file from "views"
  60. });
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top