2017-06-11 8 views
-1

node.jsでチャットルームを実装したいと思いますhttpサーバに静的ファイルを追加するには?

私はsocket.ioによって提供されるテンプレートを使用します。私は、端末に

node index.js 

を入力すると

var app = require('express')(); 
var http = require('http').Server(app); 
var io = require('socket.io')(http); 
var fs = require('fs'); 

app.get('/', function(req, res) { 
    fs.readFile(__dirname + '/style.css'); // I don't know how to import the style.css to the chat.html 
    // fs.readFile(__dirname + '/images/'); // And how to add images file to the server 
    res.sendFile(__dirname + '/chat.html'); 
}); 

io.on('connection', function(socket) { 
    console.log('a user connected'); 
    socket.on('disconnect', function() { 
     console.log('user disconnected'); 
    }); 
    socket.on('chat message', function(msg) { 
     console.log('message: ' + msg); 
    }) 
    socket.on('chat message', function(msg){ 
     io.emit('chat message', msg); 
    }); 
    socket.broadcast.emit('hi'); 
}); 


http.listen(8888, function() { 
    console.log('listening on *:8888'); 
}); 

、それは今

listening on *:8888 
(node:12873) [DEP0013] DeprecationWarning: Calling an asynchronous function without callback is deprecated. 

を紹介し、サーバが動作することができますが、フォーマットがなくなっています。

これを修正する方法がわかりません。私はchat.htmlがstyle.cssで指定されたフォーマットになることを願っています。メッセージは純粋なテキストだけでなく、style.cssで指定されたフォーマットで送信することもできます。

+0

これが[マニュアルに記載されている]ものであれば(https://expressjs.com/en/starter/static-files.html)。 – Quentin

答えて

0

"静的ファイル"に使用する予定のディレクトリをノードに伝える必要があります。

あなたは

が、これは

に役立ちます願っています「パブリック」フォルダの名前であるなどのCSSまたはフロントエンドJSファイルなどの静的コンテンツを開催しますので、

const staticCont = express.static(__dirname + "/public"); 

のようにそれを行います

関連する問題