when run server.js error in console http://localhost:8080/style.css // http://localhost:8080/style.css
i have catalog structure follows: have catalog structure follows:
|-- public |-- style.css |-- client.js |-- server.js |-- package.json
server.js
var express = require('express'); var app = express(); var http = require('http'); var port = process.env.port || 8080; var server = http.createserver(app); var io = require('socket.io').listen(server); app.use(express.static(__dirname + '/public')); app.set('view engine', 'ejs'); app.engine('html', require('ejs').renderfile); app.get('/', function (req, res) { res.render('index.html'); }); io.on('connection', function (socket) { console.log('a user connected'); socket.on('disconnect', function () { console.log('user disconnected'); }); socket.on('chat-msg', function (message) { console.log('message' + message.text); }); }); server.listen(port);
html
<!doctype html> <html> <head> <link rel="stylesheet" href="style.css" type="text/css"/> <title>socket.io chat</title> </head> <body> <h1>hello communication</h1> <section id="chat"> <ul id="messages"></ul> <form action=""> <input id="m"/><button>send</button> </form> </section> <script src="//code.jquery.com/jquery-1.12.0.min.js"></script> <script src="client.js"></script> <script src="http://localhost:8080/socket.io/socket.io.js"></script> <script> var socket = io.connect('http://localhost:8080'); socket.on('message',function(message){ console.log('le serveur dit'+message); }) </script> </body> </html>
client.js
var socket = io(); $('form').submit(function(e){ e.preventdefault(); // on évite le recharchement de la page lors de la validation du formulaire var message={ text : $('#m').val() } socket.emit('chat-msg',message); // on émet l'événement avec le message associé $('#m').val(''); //on vide le champ text if(message.text.trim().length !== 0){//gestion message vide socket.emit('chat-msg',message); } $('#chat input').focus(); });
how running server.js?
if in public
directory , run node server.js
don't think path work correctly
app.use(express.static(__dirname + '/public'));
as public
directory doesn't exist
Comments
Post a Comment