On my debian server, I installed node and then started node server on port 3000. The server is running, but it isn’t visible from the browser
Now when I try to get it running via my domain or via my ip(for example xx.xxx.xx.xx:3000) or my domain (my-domain.com:3000) in both cases it doesn’t work. I think I don’t quite get the concept and I tried to search for a billion different things, but I can’t find the solution to my problem. Could someone tell me, if I need to setup something else, too?
My server js code is
const express = require('express');
const app = express();
const http = require('http');
const server = http.createServer(app);
const { Server } = require("socket.io");
const io = new Server(server);
server.listen(3000, () => {
console.log('listening on *:3000');
});
io.on('connection', function (socket) {
socket.on( 'new_message', function( data ) {
io.sockets.emit( 'new_message', {
message: data.message,
date: data.date,
msgcount: data.msgcount
});
});
});
2
Answers
why are you using express and http both packages.
you can run server by either of them.
and then add a get route for it.
I hope this will work!
You need to listen for GET requests in order to respond to them.
Try adding something like:
In your case make sure you add the route before passing the
app
to thehttp.createServer()
method, or otherwise just use something likeapp.listen(3000)
.More info in the docs: https://expressjs.com/en/guide/routing.html