我有以下NodeJS代码。
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 !");
});
但是当我运行代码时出现错误:
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)
请帮我识别代码中的语法错误。
答案 0 :(得分:0)
我找到了问题的答案,我们需要更新nodejs stable
sudo npm clean -f
sudo npm install -g n
sudo n stable
这些命令将下载新的稳定版NodeJS。使用这些代码更新NodeJS后,没有问题。