I have a simple one-page screenshot app using puppeteer/headless chrome that works fine in localhost.
When I run it in azure, page fails to load due to server error.
When I check the logs, I get 'unexpected token' errors on what looks like valid javascript and must be valid since it runs fine local.
Using the line numbers in error logs as a guide,
I re-wrote the code in different formats, trying to work-around the issue.
Conde runs fine local every time, but in azure, the exception just moves to a different line.
Has anyone else encountered this issue?
code of file that is throwing errors:
'use strict';
var express = require('express');
var AWS = require('aws-sdk');
var awsConfig = require('aws-config');
var http = require("http");
var fs = require('fs')
var request = require("request");
var router = express.Router();
var puppeteer = require('puppeteer');
var screenshot = async function (req) {
var appUrl = "x";
if (req.query.localHost) {
appUrl = "x";
}
var url = appUrl + "x";
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.setViewport({ width: 1600, height: 900 });
var path = "x";
try {
await page.goto(url, { waitUntil: 'load' });
page.on('console', function (msg) {
if (msg.text === "all are done") {
(async () => {
// console.log(msg.text);
await page.screenshot({ path: path, type: "jpeg", quality: 90 });
var fileName = "x";
saveAutoGenThumbnail(fileName, fs.createReadStream(path), function (err, data) {
if (err) {
} else {
//fs.unlink(path);
fs.unlinkSync(path);
}
});
await browser.close();
})();
}
});
} catch (e) {
console.error(e);
}
}
function saveAutoGenThumbnail(imageName, imageFile, callBack) {
AWS.config.accessKeyId = "x";
AWS.config.secretAccessKey = "x";
AWS.config.region = "x";
var s3 = new AWS.S3();
var s3Bucket = new AWS.S3({ params: { Bucket: 'x' } })
var data = {
Key: imageName, Body: imageFile, ACL: 'public-read', ContentType: "image/jpeg"
};
s3Bucket.putObject(data, callBack);
}
/* GET home page. */
router.get('/', function (req, res) {
// screenshot(req);
res.render('screenshot', { title: 'screenshot' });
});
module.exports = router;
web config:
<?xml version="1.0" encoding="utf-8"?>
<!--
For more information on how to configure your Node.js application, please visit
http://go.microsoft.com/fwlink/?LinkId=290972
-->
<configuration>
<appSettings>
</appSettings>
<system.webServer>
<!-- mimeMap enables IIS to serve particular file types as specified by fileExtension. -->
<staticContent>
<mimeMap fileExtension=".svg" mimeType="image/svg+xml" />
</staticContent>
<modules runAllManagedModulesForAllRequests="false" />
<!-- Web.Debug.config adds attributes to this to enable remote debugging when publishing in Debug configuration. -->
<iisnode watchedFiles="web.config;*.js;routes*.js;views*.pug"/>
<handlers>
<add name="iisnode" path="app.js" verb="*" modules="iisnode" />
</handlers>
<security>
<requestFiltering>
<hiddenSegments>
<remove segment="bin" />
</hiddenSegments>
</requestFiltering>
</security>
<rewrite>
<rules>
<clear />
<!-- Don't interfere with requests for node-inspector debugging -->
<rule name="NodeInspector" patternSyntax="ECMAScript" stopProcessing="true">
<match url="^server.js/debug[/]?" />
</rule>
<rule name="app" enabled="true" patternSyntax="ECMAScript" stopProcessing="true">
<match url="iisnode.+" negate="true" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
<action type="Rewrite" url="app.js" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…