Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6176411
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T00:05:53+00:00 2026-05-24T00:05:53+00:00

I am new to node.js. I am having a few problems in the code

  • 0

I am new to node.js. I am having a few problems in the code i am trying. Take a look at the code:

var http =require('http');
var url = require('url');
var events=require('events');
var e=new events.EventEmitter();

var i=0;
var clientlist=new Array();

function user(nam,channel) {
this.nam = nam;
this.chan=channel;
}

server = http.createServer(function(req,res) {
res.writeHead(200,{'Content-Type':'text/html'});
res.write('welcome');

var pathname = url.parse(req.url).pathname;
pathname=pathname.substring(1);
pathnames=pathname.split("&");
var c=new user(pathnames[0],pathnames[1]);
clientlist[i++]=c;

console.log("user "+pathnames[0]+" joined channel "+pathnames[1]);

e.emit('userjoined',clientlist[i-1].nam,clientlist[i-1].chan);

e.on('userjoined',function(n,c) {
res.write("new user joined with name: "+n+" and he joined channel "+c+"\n");
});

});
server.listen(2000);

The problems i am having are:

  1. I dont get a welcome message in browser for this line of code: res.write(“welcome”); But,i get the console.log() message below it in the terminal

  2. The userjoined event that i emitted is not caught. but, after i close the server, everything happens at once. I get the welcome message in the browser, and the callback for the userjoined event.

Can someone tell me what is going wrong here? Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T00:05:55+00:00Added an answer on May 24, 2026 at 12:05 am

    ok there are several issues:

    1. you need to declare the e.on userjoined before you call it
    2. you need a res.end() in the e.on userjoined.

    Here is the code fixed:

    var http =require('http');
    var url = require('url');
    var events=require('events');
    var e=new events.EventEmitter();
    
    var i=0;
    var clientlist=new Array();
    
    function user(nam,channel) {
    this.nam = nam;
    this.chan=channel;
    }
    
    e.on('userjoined',function(res,n,c) {
    console.log("iuser "+pathnames[0]+" joined channel "+pathnames[1]);
    res.write("new user joined with name: "+n+" and he joined channel "+c+"\n");
    res.end();
    });
    
    server = http.createServer(function(req,res) {
    res.writeHead(200,{'Content-Type':'text/html'});
    res.write('welcome');
    
    var pathname = url.parse(req.url).pathname;
    pathname=pathname.substring(1);
    pathnames=pathname.split("&");
    
    var c=new user(pathnames[0],pathnames[1]);
    clientlist[i++]=c;
    
    console.log("user "+pathnames[0]+" joined channel "+pathnames[1]);
    
    e.emit('userjoined',res,clientlist[i-1].nam,clientlist[i-1].chan);
    
    });
    server.listen(2000);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to add a new node to an jQuery SimpleTree , but all
I am having problems getting the desired behavior out of these few classes and
New to javascript/jquery and having a hard time with using this or $(this) to
New to Linux programming in general. I am trying to communicate with a kernel
In new C++ code, I tend to use the C++ iostream library instead of
I've been trying to find an answer to this question for a few hours
I have a fairly new drupal installation with a few hundred nodes. I moved
I am having a treeview initially with a root node when form is loaded.
I'm having alot of trouble with my code. When I compile I get the
I am having a treeview with one root node . I have written MouseHoverEvent

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.