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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T03:50:54+00:00 2026-05-20T03:50:54+00:00

I have written a http server using node js var sys = require(sys), http

  • 0

I have written a http server using node js

var sys = require("sys"),
    http = require("http"),
    url = require("url"),
    path = require("path"),
    fs = require("fs");



http.createServer(function(request, res) {

    var parsed_url = url.parse(request.url);
    var uri = parsed_url.pathname;
   if(uri === "/test"){

  res.writeHead(200, {'Content-Type': 'text/javascript'});

      request.addListener('data', function (chunk) {
          var data = eval("(" + chunk + ")");
          console.log(data[0].id);
      })
      request.addListener('end', function() {
          console.log('end triggered');
          res.write("Post data");
          res.end();
      }); 
   }
}).listen(8080);

and i am trying to send back response of ajax request but i am unable to receive any response. Here is the code for ajax request ,

    var myhttp = new XMLHttpRequest();
    var url = "http://localhost:8080/test";

    var data = [{"a":"1"},{"b":"2"},{"c":"3"}];

    var dataJson = JSON.stringify(data);
    myhttp.open('POST', url, true);
    myhttp.send(dataJson);
    myhttp.onreadystatechange = function() {        
        if ((myhttp.readyState == 4) && (myhttp.status == 200)){
            alert(myhttp.responseText);
        }
        else if ((myhttp.readyState == 4) && (myhttp.status != 200))
        {
            console.log("Error in Connection");
        }

Can anyone help me what i am doing wrong …

Thanks
Vinay

  • 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-20T03:50:54+00:00Added an answer on May 20, 2026 at 3:50 am

    You have to read the data in a different way. Posted data arrives on a node server in chunks (the ‘data’ event), that have to be collected until the ‘end’ event fires. Inside this event, you are able to access your payload.

    var body = '';
    
    request.addListener('data', function (chunk) {
      body += chunk;
    });
    
    request.addListener('end', function() {
      console.log(body);
      res.write('post data: ' + body);
    });
    

    Additionaly, there seem to be some issues with your client-side code (especially concerning the status-code checks), but i can’t really help you with those as i always work with frameworks like jQuery to manage async requests.

    If you want to build reliable node.js servers for web use, i highly recommend the high-performance HTTP-Framework Express. It takes away alot of the pain when developing a web-based server application in node and is maintained actively.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Looking for a bit of help, I have currently written a HTTP server. It
I have an in-house HTTP server written in Java; full source code at my
I've written a dummy http server as an exercise in F#. I'm using Mono
I have written a facelets web application using tomcat as a application server. My
I have written a small HTTP server and everything is working fine locally, but
I have written an AIR Application that downloads videos and documents from a server.
I have written an HTTP proxy that does some stuff that's not relevant here,
I am writing a little app similar to omegle. I have a http server
I have written a small client server socket application. It is a proof of
Question: I have written a console program that uses the SQL server 2005 web

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.