【问题标题】:NodeJS Syntax issueNodeJS 语法问题
【发布时间】:2019-12-29 06:25:15
【问题描述】:

我的节点 JS 代码有问题,我收到错误消息:

/root/dev/remote/remote.js:43
.on('connection', async (socket) => {
                        ^

SyntaxError: Unexpected token (
    at createScript (vm.js:56:10)
    at Object.runInThisContext (vm.js:97:10)
    at Module._compile (module.js:549:28)
    at Object.Module._extensions..js (module.js:586:10)
    at Module.load (module.js:494:32)
    at tryModuleLoad (module.js:453:12)
    at Function.Module._load (module.js:445:3)
    at Module.runMain (module.js:611:10)
    at run (bootstrap_node.js:394:7)
    at startup (bootstrap_node.js:160:9)
Launching Chromium
[486:527:0824/012248.628260:ERROR:bus.cc(394)] Failed to connect to the bus: Could not parse server address: Unknown address type (examples of valid types are "tcp" and on UNIX "unix")

(google-chrome:486): LIBDBUSMENU-GLIB-WARNING **: 01:22:49.591: Unable to get session bus: Unknown or unsupported transport ?disabled? for address ?disabled:?
[486:610:0824/012250.491318:ERROR:object_proxy.cc(619)] Failed to call method: org.freedesktop.DBus.Properties.Get: object_path= /org/freedesktop/UPower: org.freedesktop.DBus.Error.ServiceUnknown: The name org.freedesktop.UPower was not provided by any .service files
[486:610:0824/012250.493116:ERROR:object_proxy.cc(619)] Failed to call method: org.freedesktop.UPower.GetDisplayDevice: object_path= /org/freedesktop/UPower: org.freedesktop.DBus.Error.ServiceUnknown: The name org.freedesktop.UPower was not provided by any .service files
[486:610:0824/012250.497596:ERROR:object_proxy.cc(619)] Failed to call method: org.freedesktop.UPower.EnumerateDevices: object_path= /org/freedesktop/UPower: org.freedesktop.DBus.Error.ServiceUnknown: The name org.freedesktop.UPower was not provided by any .service files
^C[509:509:0824/012302.391101:ERROR:x11_util.cc(109)] X IO error received (X server probably went away)
[486:486:0824/012302.391250:ERROR:chrome_browser_main_extra_parts_x11.cc(62)] X IO error received (X server probably went away)

但是,这个语法非常好。

我尝试过重新安装节点/使用不同的版本,但我不确定这是否与此有关。

这里是 remote.js(还有更多,这只是相关区域)

const server = require('http').createServer();
const socketIO = io(server);

socketIO
.on('connection', async (socket) => {
    socket
    .use((socket, next) => {
        if(true) return null;
        if(socket.handshake.query && socket.handshake.query.token){
            jwt.verify(socket.handshake.query.token, 'SECRET_KEY', (err, decoded) => {
                if (err) return next(new Error('Authentication error'));
                socket.decoded = decoded;
                next();
            });
        }else{
            next(new Error('Authentication error'));
        }
    })

有人知道这是什么吗?我试图在我的 Centos 7 VPS 上将我的 chromium 实例显示到浏览器中并通过浏览器控制它。

【问题讨论】:

  • 什么版本的node.js?该错误看起来像是不知道async 或胖箭头函数的旧版本。您可以尝试删除两者(替换为经典功能),看看是否会出现该错误。
  • @jfriend00 我在节点 v6.17.1 上尝试现在,但我认为这不会解决它:(
  • @jfriend00 我刚刚意识到我的节点严重过时,感谢 centos。打算尝试更新或弄清楚如何。
  • 我不认为 node.js v6 支持 async 没有特殊的命令行标志。如果您想使用现代语言功能,绝对值得获得更新版本的 node.js。
  • @jfriend00 你是对的!谢谢 :) 我更新了,所有错误都消失了!谢谢!

标签: node.js chromium


【解决方案1】:

默认情况下,您的旧节点 v6.17 将不支持 async 关键字(ES7 功能)。有一个命令行开关可能会启用一些较新的功能,但老实说,那个版本有点旧。

由于 node.js 现在已达到 v12 的稳定版本,如果您想使用最新的语言功能,您可能需要升级到更新的 node.js 版本。

【讨论】:

    猜你喜欢
    • 2011-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-15
    • 2011-09-21
    • 2019-11-07
    • 2016-04-30
    相关资源
    最近更新 更多