简体   繁体   中英

ECMAScript 6 support for node.js and its different packages like express body_parser natively

I am trying to migrate old nodejs (version 6.x) rest service project to latest nodejs (version 9.6.1) rest service project and want to follow ECMAScript 6 specification without any transplier.

The current project uses express, body-parser, moment, multer, cors, ethereumjs-tx, secp256k1, requirejs and some other nodejs modules, so my question is, does all these different modules as well as nodejs support ECMAScript 6 specification natively.

In internet I saw lots of solutions using bable transplier but I do not want to follow that approach.

Again I found https://github.com/mench/express-restful-es6 a ECMAScript 6 implementation of express but again it does not work. It failed on default import and use of decoration with @.

So is the ECMAScript 6 support for node.js and it modules are already implemented or should I wait for some time.

EDIT

I agree the decorator patter support is not in ECMAScript 6, but import and export are also not working, I tried to run the following code ...

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 .....")

but got following error...

> 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.

Again @lorefnon if a module does not support ECMAScript 6 style import/export will I be able to import it in my code, for example if I try to work with lastest expression.js can I import it with ECMAScript 6 syntax without any transplier.

ES6 is fully backward compatible. So, libraries and SDKs don't have to do anything additional to be ES6 compliant.

Of course, they may (or may not) choose to use ES-Next features, but it is common practice that any libraries using features not supported in the versions of the node they target, transpile their source as a part of their publishing process so consumers don't have to bother with transpiling of libraries.

If you ensure that your application uses the ES6 features supported by node you don't have to use a transpiler.

Again I found https://github.com/mench/express-restful-es6 a ECMAScript 6 implementation of express but again it does not work. It failed on default import and use of decoration with @.

Their examples use the decorator syntax currently not supported by Node.

However if you want to avoid transpilation you can use the exposed functions as plain functions instead of decorators:

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

}

is basically syntax sugar for:

class ApiResource_ {
    use(){
        //authorize
        console.info("authorize");
        this.next();
    }
}

const ApiResource = Rest('/api')(ApiResource_)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM