skip to Main Content

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
            });
    });
});

Error i got

2

Answers


  1. 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.

    import { createServer } from "http";
    import { Server } from "socket.io";
    
    const httpServer = createServer();
    const io = new Server(httpServer, {
      // ...
    });
    
    io.on("connection", (socket) => {
      // ...
    });
    
    httpServer.listen(3000);
    
    

    I hope this will work!

    Login or Signup to reply.
  2. You need to listen for GET requests in order to respond to them.

    Try adding something like:

    app.get('/', function (req, res) {
      res.send('GET request test.')
    })

    In your case make sure you add the route before passing the app to the http.createServer() method, or otherwise just use something like app.listen(3000).

    More info in the docs: https://expressjs.com/en/guide/routing.html

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search