2017-08-08 67 views
0

我開始了服務器的NodeJS但如下所示無法連接: Node server not showing index.js message here[![][1]節點服務器啓動,但不能連接到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()); 
}); 
+0

你嘗試從本地主機訪問特定的路線&方面? – pdjota

回答

0

的線索是在瀏覽器的錯誤 - 連接超時,你永遠只能出現發送響應返回給客戶端上GET請求。但是,很多時候客戶端首先會發送自檢請求,例如, OPTIONS/HEAD

試着改變你的代碼

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.write('<p>Some random text.</p>'); 
    } 
    return res.end(); // ensure all requests return a response 
}); 

FWIW,你應該結帳Express,將讓您的生活更容易瞄準服務HTML等

相關問題