Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
174 views
in Technique[技术] by (71.8m points)

node.js - nodejs azure application throwing errors about javascript syntax that do not occur in localhost environment

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Two things:

  1. Async functions are not supported by Node.js versions older than version 7.6.

  2. Azure Web Apps currently doesn't support Node.js 8.9.1. You can check all available versions in Azure via https://<yourappname>.scm.azurewebsites.net/api/diagnostics/runtime.

So, to avoid this error, you'll need to set the Node.js version to 8.9.0 instead:

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...