我使用Express.js和Socket.io开发聊天应用程序。
一开始,我使用Express-Generator创建我的项目,并使用node ./bin/www脚本启动,但是我删除了所有的./ bin / www文件并将其与app.js结合起来。
我在生产模式中使用HTTPS,在开发模式中使用HTTP。
因此,我编写了以下代码。
[app.js]
require('dotenv').config()
const express = require('express');
const path = require('path');
const fs = require('fs')
const app = express();
let io = require('socket.io')(app) // I Attach (app) here!!
const https = require('https')
const http = require('http')
const PORT = process.env.PORT || 443;
const domain = 'example.com';
const option = process.env.NODE_ENV === "production" ? {
ca: fs.readFileSync('/etc/letsencrypt/live/' + domain + '/fullchain.pem'),
key: fs.readFileSync(path.resolve(process.cwd(), '/etc/letsencrypt/live/' + domain + '/privkey.pem'), 'utf8').toString(),
cert: fs.readFileSync(path.resolve(process.cwd(), '/etc/letsencrypt/live/' + domain + '/cert.pem'), 'utf8').toString(),
} :
undefined;
// In Production Mode use HTTPS Server
// In Development Mode use HTTP Server
/* HTTPS Server */
option
?
https.createServer(option, app).listen(PORT, () => {
console.log(`Server is running at port ${PORT}`);
}) :
undefined;
/* HTTP Server */
option
?
http
.createServer(function (req, res) {
res.writeHead(301, {
Location: "https://" + req.headers["host"] + req.url
});
res.end();
})
.listen(80) :
http.createServer(app).listen(PORT, () => {
console.log(`Server is running at port ${PORT}`);
});
当我运行应用程序时,会出现以下错误。
Users\super\Downloads\project\build\node_modules\socket.io\lib\index.js:258
throw new Error(msg);
^
Error: You are trying to attach socket.io to an express request handler function. Please pass a http.Server instance.
at Server.listen.Server.attach (C:\Users\super\Downloads\project\build\node_modules\socket.io\lib\index.js:258:11)
at new Server (C:\Users\super\Downloads\project\build\node_modules\socket.io\lib\index.js:59:17)
at Server (C:\Users\super\Downloads\project\build\node_modules\socket.io\lib\index.js:44:41)
at Object.<anonymous> (C:\Users\super\Downloads\project\build\app.js:13:30)
at Module._compile (internal/modules/cjs/loader.js:1156:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1176:10)
at Module.load (internal/modules/cjs/loader.js:1000:32)
at Function.Module._load (internal/modules/cjs/loader.js:899:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12)
at internal/main/run_main_module.js:18:47
Program node app.js exited with code 1
错误信息说应该附加服务器实例,因此我修改了我的代码。但是我多次修改.createServer行,但它仍然无法工作。
但是当我使用另一个端口(例如3000)时,它就正常工作。但我想要使用相同的端口。
而且我将socket Io代码放在同一个app.js中。
这是socketio代码的一部分。
/* Socket IO Functions */
io.on('connection', function (socket) {
// Join Room Scoket
socket.on('JoinRoom', function (data) {
socket.leave(`${data.leave}`)
// console.log(`Leave ROOM : ${data.leave}`)
socket.join(`${data.joinedRoomName}`);
// console.log(`NEW JOIN IN ${data.joinedRoomName}`)
// console.log(`RECEIVER : ${data.receiver}`)
// When Reads the message SET notice to '1'
// db.query(`UPDATE chatData SET notice='1' WHERE chatReceiver=? AND roomName=?`, [data.receiver, data.joinedRoomName])
Chat.aggregate([{
$match: {
'chatReceiver': data.receiver,
'roomName': data.joinedRoomName,
'chatNotice': 1
}
},
{
$set: {
'chatNotice': 0
}
}
], (err, result) => {
if (err) throw err;
// console.log(result);
})
})
[前端代码]
/* Global */
let socket = io.connect('http://127.0.0.1:3000'); // But I want to use 80 or 443 port
我该如何解决这个问题?