Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.00 KB | None | 0 0
  1. const express=require('express');
  2. const app = express();
  3. var fs = require("fs");
  4. var jsdom = require('jsdom');
  5. var db
  6.  
  7. app.set('view engine','ejs');
  8. app.use(express.static('public'));
  9.  
  10.  
  11. app.get('/',function(req, res){
  12. var htmlSource = fs.readFileSync(__dirname + '/views/index.ejs', "utf8");
  13. call_jsdom(htmlSource, function (window) {
  14.  
  15. var $ = require('jquery')(window);
  16.  
  17. console.log($('body').html()); // THIS CONTAINS HTML WITH jQUERY ADDED
  18.  
  19. db.collection('quotes').find().toArray(function(err,result){
  20. if(result.length>0)
  21. res.render('index.ejs', {quotes:result})
  22. else
  23. res.render('index.ejs')
  24. })
  25. });
  26.  
  27. function call_jsdom(source, callback) {
  28.  
  29. jsdom.env(
  30. source,
  31. jquery-1.10.2.js, // (*)
  32. function(errors, window) { // (**)
  33. process.nextTick(
  34. function () {
  35. if (errors) {
  36. throw new Error("There were errors: "+errors);
  37. } callback(window);
  38. });
  39. });
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement