server.js
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
app.get('/', function(req, res){
res.sendFile(__dirname + '/index.html');
});
io.on('connection', function(socket){
socket.on('chat message', function(msg){
io.emit('chat message', msg);
});
});
http.listen(3000, function(){
console.log('listening on *:3000');
});
index.html
<!doctype html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Socket.IO chat</title>
<link href="style.css" rel="stylesheet" type="text/css">
</head>
<body>
<div id="chat">
<ul id="messages"></ul>
<input id="m" autocomplete="off">
<div id="button">Send</div>
</div>
<script src="https://cdn.socket.io/socket.io-1.2.0.js"></script>
<script src="http://code.jquery.com/jquery-1.11.1.js"></script>
<script>
var socket = io();
$('#button').on('click touchstart', function(){
socket.emit('chat message', $('#m').val());
$('#m').val('');
return false;
});
socket.on('chat message', function(msg){
$('#messages').append($('<li>').text(msg));
});
</script>
</body>
</html>
I start my server on my desktop PC runing windows 8.0 x64 and i forward port on router so then i connect for example from my phone going to xxx.xxx.xxx.xxx:3000 (current home ip address) all was working ok until i wanted to include external jquery from my "server" and css file style.css also from the server. I was reading on stack that there is some method called static path so i created inside my project folder "public" and i used:
app.use(express.static(path.join(__dirname, 'public')));
which i found here: Node.js - external JS and CSS files (just using node.js not express)
but not single one method presented on stack worked, it keep returning several errors:
is not an object
is not defined
or just server starts but file is not found
my hd path is:
C:\Users\Patryk\Desktop\nodeproject\index.html
C:\Users\Patryk\Desktop\nodeproject\server.js
C:\Users\Patryk\Desktop\nodeproject\node_modules\...
C:\Users\Patryk\Desktop\nodeproject\public\...
It sounds like you might not have things in the proper order. You need to declare your static paths before your routes. Something like this:
var express = require('express');
var app = express();
app.use(express.static(path.join(__dirname, 'public')));
app.get('/', function(req, res){
res.sendFile(__dirname + '/index.html');
});
Here is an updated version of your server.js
file:
var express = require('express');
var app = express();
var http = require('http').Server(app);
var io = require('socket.io')(http);
app.use(express.static(path.join(__dirname, 'public')));
app.get('/', function(req, res){
res.sendFile(__dirname + '/index.html');
});
io.on('connection', function(socket){
socket.on('chat message', function(msg){
io.emit('chat message', msg);
});
});
http.listen(3000, function(){
console.log('listening on *:3000');
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.