0
我開始了服務器的NodeJS但如下所示無法連接: 節點服務器啓動,但不能連接到IP
在它顯示了運行控制檯,但在瀏覽器,它是喜歡它無法連接。 指數的代碼,因爲它遵循:
console.log('Hiiiiiiiiiiiiiiiiiiii');
var io = require('socket.io');
var dot = require('dotenv').config();
var PORT = 3001;
var express = require('express');
var app = express();
var mysql = require('mysql')
var connection = mysql.createConnection({
host: 'localhost',
user: 'titan_bidwork',
password: '[email protected]#',
database: 'titan_bidwork'
})
connection.connect(function(err) {
if (err) throw err
console.log('You are now connected...')
})
http = require('http'),
server = http.createServer(function(req, res) {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Request-Method', '*');
res.setHeader('Access-Control-Allow-Methods', 'OPTIONS, GET');
res.setHeader('Access-Control-Allow-Headers', '*');
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
if (req.method === 'GET') {
res.writeHead(200, 'Hi, this is a very bad practice.', {
'Content-Type': 'text/html'
});
res.write('<h1>Node Server Running....</h1>');
res.end('<p>Some random text.</p>');
return;
}
});
io = io.listen(server)
io.on('connection', function(socket) {
socket.on('subscribe', function(room) {
console.log('joining room', room);
socket.join(room);
});
socket.on('message', function(msg) {
console.log('message--->>', msg);
console.log('msg----=>', msg["room"], msg["message"], msg["flag"]);
connection.query("INSERT INTO chat_room (room, message, flag) VALUES (?,?,?)", [msg['room'], msg['message'], msg['flag']], function(err, result) {
if (err) throw console.log(err)
})
io.to(msg.room).emit('message', msg);
});
socket.on('connect_failed', function(msg) {
console.log('connection failed', msg);
});
socket.on('send message', function(data) {
console.log('sending room post', data.room);
// io.to(room_name).emit('chat message', msg);
socket.broadcast.to(data.room).emit('conversation private post', {
message: data.message
});
});
});
app.post('/getMsgs', function(req, res) {
console.log(req, '----', res);
})
server.listen(PORT, function() {
console.log(server.address());
});
你嘗試從本地主機訪問特定的路線&方面? – pdjota