How to display logs in node.js? - node.js

I run my application is written in Node.js using CMD (Windows OS).
node server.js
There is console.log('Client connected...'); in server file.
But I dont see logs in CMD whene I application is working. How can I fix it?
My code is:
var express = require('express');
var app = express();
var server = require('http').createServer(app);
var io = require('socket.io')(server);
app.use(express.static(__dirname + '/bower_components'));
app.get('/', function(req, res,next) {
res.sendFile(__dirname + '/index.html');
});
server.listen(4200);
io.on('connection', function(client) {
console.log('Client connected...');
});

Related

How to test Socket.io and Express on a DOMAIN

I am trying to run Node.js on a domain that I do NOT own (basically a snadbox), I figured the problem is that socket.io and express are not running. The domain I have access to upload files to is https://'Domain'.com/MyID (domain is in quotes so it doesn't make a link here)
The code shown is what runs perfectly on localhost NOT ON A DOMAIN, this is my first experience with node thus I do not know what to do after weeks of videos that only show it running on localhost.
var express = require('express');
var app = express();
var serv = require('http').Server(app);
app.get('/',function(req, res) {
res.sendFile(__dirname + '/client/index.html');
});
app.use('/client',express.static(__dirname + '/client'));
serv.listen(2000);
console.log("Server started.");
So I found this
const http = require('http');
const hostname = '127.0.0.1';
const port = 3000;
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello World\n');
});
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
And I'm wandering if I can set hostname to a domain directory (directory being my access to edit /MyID)
Before edit:
I tried changing the requirements in the code above but that did absolutely nothing
var express = require('express');
var app = express();
var serv = require('https://'example'.com/MyID').Server(app);
app.get('/',function(req, res) {
res.sendFile(__dirname + '/client/index.html');
});
app.use('/client',express.static(__dirname + '/client'));
serv.listen(2000);
console.log("Server started.");
Please help me I do not know what to do.

HTML changes not reflecting when running using node and nodemon

I am trying to send my HTML file as a response form my server.js, but it is not adapting to my HTML changed even after restarting the server.
I have also tried nodemon.
var express = require('express');
var app = express();
var server = require('http').createServer(app);
var io = require('socket.io')(server);
app.get('/', function(req, res, next) {
res.sendFile(__dirname + '/public/index.html');
});
app.use(express.static('public'));
app.set('view cache', true);
io.on('connection', function(client) {
console.log('Client connected...');
client.on('join', function(data) {
console.log(data);
});
client.on('messages', function(data){
client.emit('thread', data);
client.broadcast.emit('thread', data);
});
});
server.listen(7777);

Socket.io not working with Heroku

I have looked at all of the Stack Overflow questions regarding the problem. Still, I am unable to run Socket IO along with Heroku. I have looked at https://github.com/socketio/socket.io/issues/1542 and this https://github.com/nodejs/node-v0.x-archive/wiki/Socket.IO-and-Heroku. The sockets run locally.
index.js
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var port = process.env.PORT || 8080;
app.get('/', function(req, res){
res.sendFile(__dirname + '/index.html');
});
io.on('connection', function(socket){
io.set("transports", ["xhr-polling"]);
io.set("polling duration", 10);
});
var server = http.listen(port, function(){
});
The running sample is here. https://curly-octo-woof.herokuapp.com/

using socket.io in conjunction with express

I am trying to sent a basic message when users connect and disconnect. Later i want to sent parameters back and forth. But with the following code i do not get the messages print out on the server when they connect/disconnect.
When i replace io.on with server.on i can get the message 'User connected' but not user disconnected when i refresh/close the page. This however is buggy, since not everytime i refresh it logs the message and on first time it sometimes log the message multiple times.
server.js
var //io = require('socket.io'),
http = require('http'),
express = require('express');
var PORT = process.env.PORT || 8080,
HOST = process.env.HOST || 'localhost';
var app = express();
app.use(express.static(__dirname + '/views'));
app.get('/', function (req, res) {
res.sendfile(__dirname + 'index.html');
});
server = http.Server(app);
//io = io.listen(server);
var io = require('socket.io')(server);
io.on('connection', function(socket){
console.log('User connected');
io.on('disconnect', function(socket) {
console.log('User Disconnected');
});
});
server.listen(PORT, HOST, null, function() {
console.log('Server listening on port %d in %s mode', this.address().port, app.settings.env);
});
New code:
var express = require('express'),
app = express(),
http = require('http'),
server = http.createServer(app),
io = require('socket.io').listen(server);
var PORT = process.env.PORT || 8080,
HOST = process.env.HOST || 'localhost';
server.listen(PORT);
app.use(express.static(__dirname + '/views')); //need this for my directory structure.
app.get('/', function (req, res) {
res.sendfile(__dirname + 'index.html');
}); //Need this to call my page.
io.on('connection', function( client ){
console.log('Connected');//this does not work.
io.on('disconnect', function( client) {
console.log(client);
});
});
Try reconfiguring your socket, and server instantiation:
var express = require('express'),
app = express(),
server = require('http').createServer(app),
io = require('socket.io').listen(server);
server.listen(process.env.PORT || 3000);
io.on('connection', function( client ) {
console.log(client) //this will now work
client.on('disconnect', function( id ) {
console.log('user with ID ' + id + ' has disconnected');
});
});
First of all you need to be sure if you have included the client side socket.io files.
<script src="/socket.io/socket.io.js"></script>
Then you need to initialize the socket connection.
<script>
var socket = io();
</script>
The above changes will be in your index.html file.
Now in server.js file,
io.sockets.on('connection', function(socket) {
console.log("User connected");
socket.on('disconnect', function() {
console.log('Got disconnect!');
});
});

Nodejs include socket.io in router page

I have an express node app, and I'm trying to keep my code neat by not having all the socket.io stuff in app.js
I don't know the best way to go about this. Here is my initial thought which doesn't feel like the cleanest one
// app.js
var express = require('express')
, app = express()
, server = require('http').createServer(app)
, url = require('url')
, somePage = require('./routes/somePage.js')
, path = require('path');
app.configure(function(){...});
app.get('/', somePage.index);
and the route
// somePage.js
exports.index = function (req, res, server) {
io = require('socket.io').listern(server)
res.render('index',{title: 'Chat Room'})
io.sockets.on('connection', function(socket) {
...code...
}
}
I feel like I'm close but not quite there
I don't know if I'm reading that right but it looks like you are starting a socket server on every request for /, which I'm frankly a little surprised works at all.
This is how I'm separating out the socket.io code from app.js (using express 3.x which is a bit different than 2.x):
// app.js
var express = require('express');
var app = express();
var server_port = config.get('SERVER_PORT');
server = http.createServer(app).listen(server_port, function () {
var addr = server.address();
console.log('Express server listening on http://' + addr.address + ':' + addr.port);
});
var sockets = require('./sockets');
sockets.socketServer(app, server);
// sockets.js
var socketio = require('socket.io');
exports.socketServer = function (app, server) {
var io = socketio.listen(server);
io.sockets.on('connection', function (socket) {
...
});
};
Hope that helps!
a similar approach is to pass app into index.js file and initiate http and socketio server there.
//app.js
//regular expressjs configuration stuff
require('./routes/index')(app); //all the app.get should go into index.js
Since app is passed into index.js file, we can do the app.get() routing stuff inside index.js, as well as connecting socketio
//index.js
module.exports = function(app){
var server = require('http').createServer(app)
,io = require('socket.io').listen(server);
app.get('/', function(req, res){
});
server.listen(app.get('port'), function(){
console.log("Express server listening on port " + app.get('port'));
});
io.sockets.on('connection', function(socket){
socket.on('my event', function(data){
console.log(data);
});
});
io.set('log level',1);
//io.sockets.emit(...)

Resources