Skip to content

Instantly share code, notes, and snippets.

@And93
Forked from ball6847/server.js
Created December 2, 2019 19:32
Show Gist options
  • Save And93/f4c093814dbf32fe719ca464422e3771 to your computer and use it in GitHub Desktop.
Save And93/f4c093814dbf32fe719ca464422e3771 to your computer and use it in GitHub Desktop.
Chrome Headless SSR for Old School
const puppeteer = require('puppeteer')
const express = require('express')
const app = express()
// page with angularjs application
const url = 'http://ng-seo.sourcelab.xyz/'
app.get('/', async function (req, res) {
const browser = await puppeteer.launch()
const page = await browser.newPage()
await page.goto(url)
// wait for element, make sure this element appears on router change completed
await page.waitForSelector('#main')
const html = await page.evaluate(() => new XMLSerializer().serializeToString(document.doctype) + document.documentElement.outerHTML)
res.send(html)
await browser.close()
})
app.listen(3000, function () {
console.log('app listening on port 3000!')
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment