Why doesn't console.log print to the CLI? - javascript

I'm following a YouTube tutorial on NodeJs, and for some reason my console.log commands unexpectedly stopped working. Here is the sample code from the tutorial as I've typed it:
const http = require('http');
const server = http.createServer();
server.on('connection', (socket) => {
console.log('New connection')
});
server.listen(3000);
console.log('Listening on port 3000...');
After saving I try using node and nodejs
node app.js
nodejs app.js
but they print nothing.

Related

Doesn't print a killed node process with node.js

I want to simulate a crash with Node.js by using process.kill. I did a basic app with express.js in terminal #1
app.js
const express = require('express');
const app = express();
app.get('/', (req, res) => {
res.send('Hi!');
});
const server = app.listen(3000, () => console.log('Server ready'));
console.log(process.pid);
process.on('SIGINT', function () {
console.log('Simulate crash');
});
Then, I'm running in another terminal (terminal #2) a node file that kill the process of terminal #1
kill.js
const fs = require('fs');
const args = process.argv.slice(2)[0];
process.kill(args, 'SIGINT');
So when I run kill.js with the following command $ node kill.js YOURPROCESSPID in the terminal #2, it didn't print the "simulate crash" in the terminal #1. I'd like to know how to make this happen. Thank you.
I also added the console down here.

How to connect to a nodejs socket server?

I am new to node JS and I am trying to create a real time application that consists of a node JS server with socket.io and a unity application that can connect to it
I created the server with the sockets in the below code :
const express = require('express');
const http = require('http');
const app = express();
const port = process.env.PORT || 9000;
const server = http.Server(app);
const io = require('socket.io')(server);
io.on('connection',(socket) => {
console.log('A user connected');
socket.on('disconnect', () => {
console.log('user disconnected');
});
})
app.get('/',(req,res) =>{
res.json({worked : 'worked'});
});
server.listen(port,() => console.log(`Listening on localhost ${port}`));
I can connect to the socket server via the nodejs client files using socket.io-client
<script>
const socket = io('http://localhost:9000');
socket.on('message',(message) => console.log(message));
</script>
But the problem is whenever I try to connect from a different client I don't receive anything in the console.
I tried to use Smart Web Socket client to debug what's happening but whenever I connect (try to)
this happens
Any help would be much appreciated and thanks in advance
So if anyone stumbles in this thread I was able to fix the problem by installing the latest version of socket.io (^3.1.0) and allowing the EIO3 connections

How can i overcome this error while creating a local host server?

here is my codeI have typed in this code and in the browser showing error
const http = require('http');
const server = http.createServer((req , res) =>{
res.end('Hello fromm the server!');
});
server.listen(8000, '127.0.0.1', () => {
console.log('Listening to request on port 8000');
})
I'm not able to recreate this issue. Have you executed your node script in your terminal?
In this example, I've name the file 'index.js'
node index.js
Your terminal should show 'Listening to request on port 8000' after you execute your node script.

How to redirect app's data stream to browser in real time?

My node app can read stream from kafka producer and console.log it to terminal in real time. But I would like to update it in my web app the same way (real time). How can I implement it?
I start my app with 'node index.js' command (or npm start).
index.js:
'use strict';
var express = require('express'),
app = express(),
server,
data = [];
...
consumer.on('message', function (message) {
console.log(message);
data.push(message);
//global.location.reload();
});
...
app.get('/', function(req, res){
res.send(data);
});
server = app.listen(3002, function(){
console.log('Listening on port 3002');
});
I think, that I need modify res.send(data) or add some code to on('message') event.
You should keep connection between client and server.
Try this socket.io package to update message in realtime.

express app server . listen all interfaces instead of localhost only

I'm very new for this stuff, and trying to make some express app
var express = require('express');
var app = express();
app.listen(3000, function(err) {
if(err){
console.log(err);
} else {
console.log("listen:3000");
}
});
//something useful
app.get('*', function(req, res) {
res.status(200).send('ok')
});
When I start the server with the command:
node server.js
everything goes fine.
I see on the console
listen:3000
and when I try
curl http://localhost:3000
I see 'ok'.
When I try
telnet localhost
I see
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'
but when I try
netstat -na | grep :3000
I see
tcp 0 0 0.0.0.0:3000 0.0.0.0:* LISTEN
The question is: why does it listen all interfaces instead of only localhost?
The OS is linux mint 17 without any whistles.
If you don't specify host while calling app.listen, server will run on all interfaces available i.e on 0.0.0.0
You can bind the IP address using the following code
app.listen(3000, '127.0.0.1');
If you want to run server in all interface use the following code
app.listen(3000, '0.0.0.0');
or
app.listen(3000)
From the documentation: app.listen(port, [hostname], [backlog], [callback])
Binds and listens for connections on the specified host and port. This method is identical to Node’s http.Server.listen().
var express = require('express');
var app = express();
app.listen(3000, '0.0.0.0');
document: app.listen([port[, host[, backlog]]][, callback])
example:
const express = require('express');
const app = express();
app.listen('9000','0.0.0.0',()=>{
console.log("server is listening on 9000 port");
})
Note: 0.0.0.0 to be given as host in order to access from outside interface

Categories