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 6008461
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T01:49:38+00:00 2026-05-23T01:49:38+00:00

How can you extract strings from messages in NodeJS? Specifically I’m modifying a simple

  • 0

How can you extract strings from messages in NodeJS? Specifically I’m modifying a simple chat room example to accept specific commands from clients.

Example:

sock.on('connection', function(client){
    var s = the string in client.message...
    if(s == "specific string"){
        //do this
    }
    else{
        //do that
    }
});

I’m new to NodeJS and the documentation has been very helpful up till now. If I’m approaching this all the wrong way I’d definitely appreciate alternative solutions. Thanks.

Edit 1: server initialization

serv = http.createServer(function(req, res){
    res.writeHead(200, {'Content-Type': 'text/html'});
    // read index.html and send it to the client
    var output = fs.readFileSync('./index.html', 'utf8');
    res.end(output);
});
// run on port 8080
serv.listen(8080);

Edit 3: I realize that I haven’t been specific enough, sorry. Here’s a link showing the tutorial I’m following: http://spechal.com/2011/03/19/super-simple-node-js-chatroom/.

Specifically I’d like to create the chatroom supplied in the tutorial (which I’ve been able to do), and then check the messages that people are broadcasting to each other to see if they contain specific strings.

For example, if a client in the chatroom submitted the string “alpha” (types alpha, presses enter), this string would be broadcasted to all the other clients and the server would respond by broadcasting the string “Alpha has been recieved.” to all of the clients as well. My exact problem (to my knowledge) is that I can’t do any kind of string comparison with the messages my event listener receives. Is it possible to extract the text entered by my chatroom members from their messages?

  • 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-23T01:49:39+00:00Added an answer on May 23, 2026 at 1:49 am

    Where is your ‘sock.on(‘data’, function(data) {})’ handler? I think the HTTP example is actually what you are looking for, listed below.

    Example (for TCP Server):

    var server = net.Server(function(socket) {
      socket.setEncoding('ascii');
    
      socket.on('data', function(data) {
        // do something with data
      });
    
      socket.on('end', function() {
        // socket disconnected, cleanup
      });
    
      socket.on('error', function(exception) {
        // do something with exception
      });
    });
    server.listen(4000);
    

    Example for HTTP Server:

    var http = require('http');
    var url = require('url');
    var fs = require('fs');
    
    var server = http.createServer(function (req, res) {
    
      // I am assuming you will be processing a GET request
      // in this example. Otherwise, a POST request would
      // require more work since you'd have to look at the
      // request body data.
    
      // Parse the URL, specifically looking at the
      // query string for a parameter called 'cmd'
      // Example: '/chat?cmd=index'
      var url_args = url.parse(req.url, true);
    
      // Should have error checking here...
      var cmd = url_args.query.cmd;
    
      res.writeHead(200, {'Content-Type': 'text/html'});
    
      var output;
      if (cmd == "index") {
        // read index.html and send it to the client
        output = fs.readFileSync('./index.html', 'utf8');
      } else if (cmd.length > 0) {
        output = "cmd was not recognized.";
      } else {
        output = "cmd was not specified in the query string.";
      }
      res.end(output);
    });
    
    server.listen(8080);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I mean a tool that can extract certain contents from old .NET 1.1 WinForm
Does anyone know of a way i can extract all jpg images from a
How can I extract the part of this stream (the one named BLABLABLA) from
Given the URL (single line): http://test.example.com/dir/subdir/file.html How can I extract the following parts using
I've a long template from which I need to extract certain strings based on
I'd like to extract a date from any string entered. But not specific to
Folks, I tired all my PHP skills to extract domain name strings from a
I have a PHP web application on an intranet that can extract the IP
How can I extract the list of available SQL servers in an SQL server
If I have the following nvarchar variable - BTA200, how can I extract just

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.