1

I just installed node and tried to write and run some programs. When I tried this example progra, I get an error.

Maybe node and npm were installed incorrectly? Maybe some necessary packages should be install?

const http = require('http');
const net = require('net');
const url = require('url');

// Create an HTTP tunneling proxy
var proxy = http.createServer( (req, res) => {
    res.writeHead(200, {'Content-Type': 'text/plain'});
    res.end('okay');
});

proxy.on('connect', (req, cltSocket, head) => {
    // connect to an origin server
    var srvUrl = url.parse(`http://${req.url}`);
    var srvSocket = net.connect(srvUrl.port, srvUrl.hostname, () => {
        cltSocket.write('HTTP/1.1 200 Connection Established\r\n' + 'Proxy-agent: Node.js-Proxy\r\n' + '\r\n');
        srvSocket.write(head);
        srvSocket.pipe(cltSocket);
        cltSocket.pipe(srvSocket);
    });
});

Why does the below error appear?


var proxy = 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:902:3 ##
4
  • 2
    In which version of Node does this happen ? Commented Feb 18, 2016 at 19:15
  • node -v returns v0.10.25 Commented Feb 18, 2016 at 21:09
  • According to the ES6 compatibility tables, arrow functions are supported only on node.js 4.0+ Commented Feb 18, 2016 at 21:38
  • Thanks jcaron for hint. Upgrading NodeJs to 4.x version solved the problem. Commented Feb 18, 2016 at 21:52

1 Answer 1

1

Try it like this;

   var requestListener = function (req, res) {
       res.writeHead(200, {'Content-Type': 'text/plain'});
       res.end('okay');
    }

    var proxy = http.createServer(requestListener);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.