-3

I have this following NodeJS code.

var url = require('url');
var path = require('path');
var url = require('url');
var fs = require('fs');
var http = require('http');
var port = process.env.port || 3000;
var pub_dir = 'public';
var server = http.createServer(function(req, res) {
    curl = url.parse(req.url, true);
    file = path.join(__dirname, pub_dir, curl.pathname);
    fs.readFile(file, function(err, data) {
        if (err) {
            res.writeHead(404, {
                'Content-type': 'text/plai'
            });
            res.end('404 not found error');
        } else {
            res.end(data);
        }
    });
});
var sio = require('socket.io');
var io = sio.listen(server);
io.of('/chat').on('connection', function() {
    socket.emit('hello', 'socket test edildi');
    socket.on('hello', function(data) {
        io.sockets.emit('hello', 'Hamiya gonderilen signal');
    });
});
server.listen(port, '127.0.0.1', function() {
    console.log("server running !");
});

But I get an error when I run the code:

const http = require('http');
      ^^^^^ SyntaxError: Use of const in strict mode.
    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 Module.require (module.js:364:17)
    at require (module.js:380:17)
    at Server.init (/home/tural/node_modules/socket.io/node_modules/engine.io/lib/server.js:119:16)
    at new Server (/home/tural/node_modules/socket.io/node_modules/engine.io/lib/server.js:65:8)
    at Function.attach (/home/tural/node_modules/socket.io/node_modules/engine.io/lib/engine.io.js:123:16)
    at Server.initEngine (/home/tural/node_modules/socket.io/lib/index.js:274:21)

Please help me identify the syntax error in the code.

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140

1 Answers1

3

I found the answer to the problem, we need update nodejs stable

sudo npm clean -f 
sudo npm install -g n 
sudo n stable

These commands will downloaded the new stable version of NodeJS. After updating NodeJS using these codes, there are no problems.

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140