Alvin Alvin - 2 months ago 14
Node.js Question

How to write module in ECMAScript 6

The below code is working with

nodejs 4.4
:

"use strict";

const test = (res) => {
return (data) => {
return res.json({"message": "testing"});
};
};

module.exports = test;


My question is using
const
correct, or is it correctly written using
ES6
?

Answer

Yes, you can use const like that. const means "the value of this variable cannot be changed" and the interpreter will complain if you try to assign a new value to it.

Is the code above "correctly written using ES6"? Depends what you mean... for example, ES6 uses export instead of module.exports, but what you've written is not wrong. After all, it works.

ES6 is not a different language - it's Javascript with some new features. It's up to you to decide how many of those features you want to use.