我不知道为什么它总是会弹出SyntaxError意外的输入结束 在Object.runInThisContext的createScript处。我尝试了很多方法来解决它,但它仍然无法正常工作!希望有人能纠正,如果我错了!谢谢!!!!!!!
在命令行上写:
npm i socket.io express -s
var fs = require('fs')
//https的一些設定
var options = {
key: fs.readFileSync('/etc/letsencrypt/live/awiclass.monoame.com/privkey.pem'),
cert: fs.readFileSync('/etc/letsencrypt/live/awiclass.monoame.com/fullchain.pem')
}
//https & socket port
var https = require('https').createServer(options);
https.listen(4040)
var io = require('socket.io')(https);
console.log("Server socket 4040 , api 4000")
//api port
var app = require('express')();
var port = 4000;
app.listen(port, function(){
console.log('API listening on *:' + port);
});
var messages = [];
//用api方式取得
app.get('/api/messages',function(req,res){
res.send(messages);
})
io.on('connection', function(socket){
//初始化...
console.log("A user connected.");
io.emit("allMessage",messages);
socket.on('sendMessage',function(obj){
//get all message!
messages.push(obj);
console.log( obj.message + " - " + obj.name )
io.emit('newMessage', obj);
})
})
错误:
SyntaxError: Unexpected end of input
at createScript (vm.js:80:10)
at Object.runInThisContext (vm.js:139:10)
at Module._compile (module.js:616:28)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
at Function.Module.runMain (module.js:693:10)
at startup (bootstrap_node.js:188:16)
at bootstrap_node.js:609:3