![](/img/trans.png)
[英]TypeScript Typeorm Node module import error: SyntaxError: Unexpected token {
[英]Unexpected token error from node module import
我有一个使用 WebSocket 库 ( ws
) 的节点应用程序,我可以在本地机器上运行这个应用程序。
但是,当我将它发布到 Azure 应用服务时,我收到相同代码的以下错误。
我已经检查过两者都运行相同版本的节点 12.13.0,我已经完成npm install
并且软件包似乎相同(无论如何发布时它们也包含在内)。 下面的错误实际上是在抱怨来自ws
模块的文件。
触发此错误的代码行是: var WebSocket = require('ws');
Wed Dec 04 2019 13:03:04 GMT+0000 (Coordinated Universal Time): Application has thrown an uncaught exception and is terminated:
SyntaxError: Unexpected token {
at exports.runInThisContext (vm.js:53:16)
at Module._compile (module.js:373:25)
at Object.Module._extensions..js (module.js:416:10)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
at Module.require (module.js:353:17)
at require (internal/module.js:12:17)
at Object.<anonymous> (D:\home\site\wwwroot\node_modules\ws\index.js:3:19)
at Module._compile (module.js:409:26)
at Object.Module._extensions..js (module.js:416:10)
Application has thrown an uncaught exception and is terminated:
SyntaxError: Unexpected token {
at exports.runInThisContext (vm.js:53:16)
at Module._compile (module.js:373:25)
at Object.Module._extensions..js (module.js:416:10)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
at Module.require (module.js:353:17)
at require (internal/module.js:12:17)
at Object.<anonymous> (D:\home\site\wwwroot\node_modules\ws\index.js:3:19)
at Module._compile (module.js:409:26)
at Object.Module._extensions..js (module.js:416:10)
作为参考,我尝试使 GitHub 存储库websockets/ws
的示例examples/server-stats/
适用于本地和 Azure 应用服务。
首先,我将其所有源代码下载到我的本地机器并在我的本地目录中运行npm install
和npm install ws
,我的本地文件结构如下图。
图 1. 我的示例server-stats
本地文件结构。
其次,我尝试使用命令node index.js
直接运行它并遇到问题Error: Cannot find module '../..'
,然后我将文件index.js
更改为代码如下。
const WebSocket = require('ws'); // The original content is `require('../..')`
然后,我运行node index.js
并打开浏览器查看结果,它工作正常。
图 2. server-stats
在本地运行良好
据我所知,Azure App Service 需要节点应用程序从代码process.env.PORT
绑定端口值并使用web.config
文件启动,所以我更改了index.js
文件,并创建了一个web.config
文件内容来自 Kudu wiki 页面Using a custom web.config for Node apps
and to use index.js
而不是server.js
,代码如下。
索引.js
const port = 8080; server.listen(port, function() { console.log('Listening on http://localhost:'+port); });
网页配置
<?xml version="1.0" encoding="utf-8"?> <!-- This configuration file is required if iisnode is used to run node processes behind IIS or IIS Express. For more information, visit: https://github.com/tjanczuk/iisnode/blob/master/src/samples/configuration/web.config --> <configuration> <system.webServer> <!-- Visit http://blogs.msdn.com/b/windowsazure/archive/2013/11/14/introduction-to-websockets-on-windows-azure-web-sites.aspx for more information on WebSocket support --> <webSocket enabled="false" /> <handlers> <!-- Indicates that the server.js file is a node.js site to be handled by the iisnode module --> <add name="iisnode" path="index.js" verb="*" modules="iisnode"/> </handlers> <rewrite> <rules> <!-- Do not interfere with requests for node-inspector debugging --> <rule name="NodeInspector" patternSyntax="ECMAScript" stopProcessing="true"> <match url="^index.js\\/debug[\\/]?" /> </rule> <!-- First we consider whether the incoming URL matches a physical file in the /public folder --> <rule name="StaticContent"> <action type="Rewrite" url="public{REQUEST_URI}"/> </rule> <!-- All other URLs are mapped to the node.js site entry point --> <rule name="DynamicContent"> <conditions> <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="True"/> </conditions> <action type="Rewrite" url="index.js"/> </rule> </rules> </rewrite> <!-- 'bin' directory has no special meaning in node.js and apps can be placed in it --> <security> <requestFiltering> <hiddenSegments> <remove segment="bin"/> </hiddenSegments> </requestFiltering> </security> <!-- Make sure error responses are left untouched --> <httpErrors existingResponse="PassThrough" /> <!-- You can control how Node is hosted within IIS using the following options: * watchedFiles: semi-colon separated list of files that will be watched for changes to restart the server * node_env: will be propagated to node as NODE_ENV environment variable * debuggingEnabled - controls whether the built-in debugger is enabled See https://github.com/tjanczuk/iisnode/blob/master/src/samples/configuration/web.config for a full list of options --> <!--<iisnode watchedFiles="web.config;*.js"/>--> </system.webServer> </configuration>
然后,我将我的所有文件和目录上传到我的 Azure WebApp,如下图所示,以便在其 Kudu 控制台中查看。
图 3. 通过 Kudu 控制台在 Azure WebApp 上的示例文件结构
在我为Application settings
添加了一个新的重新编码WEBSITE_NODE_DEFAULT_VERSION
和12.13.0
,
图 4. 通过Application settings
WEBSITE_NODE_DEFAULT_VERSION
设置节点运行时版本
我在浏览器中打开它,它确实有效。
图 5. 对于默认设置的 websocket 功能,它不起作用
上述结果有两个问题。
对于使用 websocket 的 webapp,需要在General settings
启用Web sockets
功能。
图 6. 在General settings
启用Web sockets
功能
默认情况下,Azure 为 HTTP 访问启用了 SSL,因此需要使用wss
而不是ws
作为public/index.html
文件中的 websocket url。
图 7. 在 Azure 应用服务上启用 SSL 的public/index.html
使用wss
而不是ws
。
最后,它适用于 Azure 应用服务。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.