ECMAScript 6 支持 node.js 及其不同的包,如原生的 express body_parser



我正在尝试将旧的nodejs(版本6.x)的休息服务项目迁移到最新的nodejs(版本9.6.1)休息服务项目,并希望遵循ECMAScript 6规范,没有任何转换器。

当前的项目使用express,body-parser,moment,multer,cors,ethereumjs-tx,secp256k1,requirejs和其他一些nodejs模块,所以我的问题是,所有这些不同的模块以及nodejs是否都支持ECMAScript 6规范。

在互联网上,我看到很多使用bable transplier的解决方案,但我不想遵循这种方法。

我再次发现 https://github.com/mench/express-restful-es6 ECMAScript 6 的 Express 实现,但它再次不起作用。它在默认导入和使用 @ 的装饰时失败。

ECMAScript 6 对 node 的支持也是如此.js它的模块已经实现,或者我应该等待一段时间。

编辑

我同意装饰器模式支持不在 ECMAScript 6 中,但导入和导出也不起作用,我尝试运行以下代码......

import express from 'express'
import restful from 'express-restful-es6'
var server = express()
console.log(restful)
restful.configure(server,{
dirname: '/projectlocation' + '/resources'
})
server.listen(9000)
console.log("Listening on port 9000 .....")

但出现以下错误...

> ethereum-rest-api@1.0.0 start /projectlocation/ethereum-rest-api
> /nodelocation/bin/node --experimental-modules index.mjs
(node:31753) ExperimentalWarning: The ESM module loader is experimental.
{ Rest: [Function: Rest],
middleware: [Function: middleware],
default: Restful {} }
TypeError: restful.configure is not a function
at file:///projectlocation/index.mjs:7:9
at ModuleJob.run (internal/loader/ModuleJob.js:102:14)
at <anonymous>
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! project@1.0.0 start: `/nodelocation/bin/node --experimental-modules index.mjs`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the project@1.0.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

同样@lorefnon如果一个模块不支持 ECMAScript 6 样式的导入/导出,我是否可以在我的代码中导入它,例如,如果我尝试使用最新的表达式.js我可以在没有任何转换器的情况下使用 ECMAScript 6 语法导入它吗?

ES6 完全向后兼容。因此,库和 SDK 无需执行任何其他操作即可符合 ES6 标准。

当然,他们可能会(也可能不会)选择使用 ES-Next 功能,但通常的做法是,任何使用其目标节点版本中不支持的功能的库都会在发布过程中转译其源代码,这样使用者就不必为库的转译而烦恼。

如果确保应用程序使用 node 支持的 ES6 功能,则不必使用转译器。

我再次发现 https://github.com/mench/express-restful-es6 ECMAScript 6 的 Express 实现,但它再次不起作用。它在默认导入和使用 @ 的装饰时失败。

他们的示例使用 Node 当前不支持的装饰器语法。

但是,如果要避免转译,可以将公开的函数用作普通函数而不是装饰器:

@Rest('/api')
class ApiResource {
use(){
//authorize
console.info("authorize");
this.next();
}
}

基本上是语法糖:

class ApiResource_ {
use(){
//authorize
console.info("authorize");
this.next();
}
}
const ApiResource = Rest('/api')(ApiResource_)

最新更新