0

Code was correctly but show Error on node js socket io ?

....................................................................................................................

app.js

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

app.get('/', function(req,res){
    res.sendfile('index.html');
});

var clients = 0;
io.on('connect', function(socket){
    clients++;
    io.sockets.emit('boardcast', {message: clients + ' clients connected!'});

//console.log(io.sockets)

    socket.on('disconnect', function(){
        clients--;
        io.sockets.emit('boardcast', {message: clients + ' clients connected!'});
    });
});

http.listen(3000, function(){
    console.log('start server on port :3000');
});

index.html

<html>
<head></head>
<body>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.0.4/socket.io.js"></script>

<script type="text/javascript">
var socket = io();
socket.on('boardcast', function(data){
    document.body.innerHTML = '';
    document.write(data.message);
});
</script>

<h1>hello world</h1>
</body>
</html>

.

ssh

node app.js

return value

start server on port :3000
express deprecated res.sendfile: Use res.sendFile instead app.js:6:6
7
  • 1
    express deprecated res.sendfile: Use res.sendFile instead app.js:6:6 read this you have solution, it is sendFile not sendfile Commented May 1, 2018 at 6:59
  • When i change to res.sendFile('index.html'); -------- It's show error ----------- Commented May 1, 2018 at 7:01
  • TypeError: path must be absolute or specify root to res.sendFile at ServerResponse.sendFile (/home/admin/web/my-domain.com/public_html/node_modules/express/lib/response.js:421:11) at /home/admin/web/my-domain.com/public_html/app.js:6:6 at Layer.handle [as handle_request] (/home/admin/web/my-domain.com/public_html/node_modules/express/lib/router/layer.js:95:5) at next (/home/admin/web/my-domain.com/public_html/node_modules/express/lib/router/route.js:137:13) at Route.dispatch (/home/admin/web/my-domain.com/public_html/node_modules/express/lib/router/route.js:112:3) Commented May 1, 2018 at 7:03
  • 1
    where is your index.html file located ? Commented May 1, 2018 at 7:15
  • 2
    I think...you need to change your file path res.sendFile("To located path"); Commented May 1, 2018 at 7:24

1 Answer 1

1

Putting the commands from your question into an answer.

Express are depracating res.sendfile in favour of res.sendFile.

sendFile needs the full path to the file.

Assuming that index.html is in the same folder as app.js you can use:

const path = require('path');

res.sendFile(path.join(__dirname, 'index.html'))

If you move files around in the future simple tweak path.join

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.