Error Nodejs and when to use "=>"

Asked

Viewed 101 times

1

Good afternoon,

People I am starting my studies with Node.js technology, but when I tried to write the code of a web server example:

const http = require('http');
const hostname = '127.0.0.1';
const port = 3000;

const server = http.createServer((req, res) => {
   res.statusCode = 200;
   res.setHeader('Content-Type', 'text/plain');
   res.end('Hello World\n');
});

server.listen(port, hostname, () => {
   console.log(`Server running at http://${hostname}:${port}/`);
});

My return generates the following error:

const server = http.createServer((req, res) => {
                                             ^
SyntaxError: Unexpected token >
    at Module._compile (module.js:439:25)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)
    at startup (node.js:119:16)
    at node.js:906:3

My Node.js has been downloaded from the Github source code (v0.10.29).

NOTE: I already searched the internet and I could not explain, when to use =>. I do not understand and if anyone can help me with some reference or explanation I will be pleased to know how the =>.

  • You edited the question by changing the error (!)... wasn’t that the error you had? Changing the error is hiding the problem...

  • The error remains the way it is there, the problem is that I tested with Function + Arrow and only Arrow understood? The same way the code above is written is the error.

  • Is that I had pasted the bug with "Function".

  • @drigols Leia Stack Overflow in Portuguese is a forum?. You do not need to change the title of the question to indicate that the problem has been solved. By accepting an answer you already make this statement.

2 answers

2


If you use Arrow functions you can’t use the reserved word function, as you initially had in the question.

What’s more, the version 0.10.xx node does not support Arrow functions, the full support is only from summer 6.4.0. You can see the compatibility table here: http://node.green/#ES2015-functions-Arrow-functions

That is to say:

Wrong:

const server = http.createServer(function (req, res) => {

Correct, using Arrow functions, using recent versions of Node.js:

const server = http.createServer((req, res) => {

Correct, using function:

const server = http.createServer(function (req, res){
  • Good afternoon, thanks for the "Arrow functions" part I wouldn’t know about... But what happens is that even using without "Function" the way you spoke the error remains... If you look well don’t have Function to my Arrow.

  • @drigols in error has, that’s the code that Node is using.

  • @drigols checks your code and makes sure that error does not exist elsewhere in the code, and confirms with the line that the error indicates.

1

The problem is that you are using a very old version of Nodejs

As you said yourself you are using version v0.10.29, it is from 2014 and it seems to me that only after version 4.4.5 of 2016 has ES6 support been added, which includes Arrow Function

Browser other questions tagged

You are not signed in. Login or sign up in order to post.