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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T12:36:53+00:00 2026-06-17T12:36:53+00:00

My server.js is // server.js – the outer server loop var http = require(‘http’)

  • 0

My server.js is

// server.js - the outer server loop
var http = require('http')
 ,  php = require("./phpServer");

function start() {
    function onRequest(request, response) {
        php.phpServer('D:/websites/coachmaster.co.uk/htdocs',request, response);
        response.write('Ending');
        response.end();
    }
    http.createServer(onRequest).listen(80);
    console.log("Server started.");
}

exports.start = start;

That calls php.phpServer every request with response as the 3rd param.

phpServer contains.

//
//  phpServer.js - a generic server to serve static files and
//
var fs = require('fs')
 ,  pathfuncs = require('path')
 ,  url = require('url')
 ,  mimetypes = require('./mimetypes')

function phpServer(root, request, response) {
    // serve static or pass to php. 
    var data = url.parse(request.url);
    var ext = pathfuncs.extname(data.pathname);
    fs.stat(root+request.url, function(err, stat) {
        if (err || !stat.isFile()) { // error or not file.
            console.log('404');
            response.writeHead(404);
            response.write('Not Found');
            return;
        }
        // exists - serve.
        console.log("serve("+root+request.url+", mimetypes.mimetype("+ext+"))");
        response.writeHead(200, {'Content-Type': mimetypes.mimetype(ext)});
        response.write('Somethign to serve');
        // fs.createReadStream(root+request.url).pipe(response);
    });
}

exports.phpServer = phpServer

As I see it, response is an object and is passed by reference, therefore the response.write() here should write to the response.

It doesn’t. Response here is NOT the same as response in onRequest, so nothing in phpServer is sent to the browser – not code nor content.

The console.logs come out and show what I would expect.

How can I get the object response passed so I can call write on it?

————- added later ——————-

I’ve tried to apply answers given and code for server.is now

// server.js - the outer server loop
var http = require('http')
 ,  fs = require('fs')
 ,  pathfuncs = require('path')
 ,  url = require('url')
 ,  mimetypes = require('./mimetypes')

function phpServer(root, request, res) {
    // code adapted from page 118 of Smashing Node.js by Guillermo Rauch
    // res is response provided to onRequest. 
    var data = url.parse(request.url);
    var ext = pathfuncs.extname(data.pathname);
    res.write('Start reply');
    fs.stat(root+request.url, function(err,stat) {
        // define delayed callback - reponse in scope
        if (err || !stat.isFile()) { // error or not file.
            console.log('404');
            res.writeHead(404);
            res.write('Not Found');
            res.end
            return;
        };
        // exists so serve.
        console.log("serve("+root+request.url+", mimetypes.mimetype("+ext+"))");
        res.writeHead(200, {'Content-Type': mimetypes.mimetype(ext)});
        res.write('The file contents');
        res.end;
    }   // end callback,
    );  // end fs.stat call. 
} // end phpServer

function start() {
    function onRequest(request, response) {
        phpServer('D:/websites/coachmaster.co.uk/htdocs',request, response);
    }
    http.createServer(onRequest).listen(80);
    console.log("Server started.");
}

exports.start = start;

This does not reply at all – it times out. However the call to res.writeHead will either
fail, if res is out of scope/does not exist/undefined, or succeed if re is the param passed in.

It succeeds, and is followed by write and end, so please – what have I got wrong.

If the file does not exist I get a start reply and then a timeout.

At the res.write(‘Start reply’); res is the response param, yet it isn’t later in the fs.stat call-back.

Why not?
Damn – this is frustrating.

  • 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-06-17T12:36:54+00:00Added an answer on June 17, 2026 at 12:36 pm

    The call to response.end should be moved from the onRequest function to phpServer. As it stands phpServer cannot write anything else since the stream has been closed.

    function onRequest(request, response) {
        php.phpServer('D:/websites/coachmaster.co.uk/htdocs',request, response);
    
        // response.end(); // move this to phpServer 
    }
    

    As explained in the documentation for response.end

    This method signals to the server that all of the response headers and body have been sent; that server should consider this message complete.

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

Sidebar

Related Questions

// Server.js var http = require('http'); var path = require('path'); var fs = require('fs');
Server 1 Pings/Request URL http://website.com/page.php?code=NeedThat Server 2 page.php grabs NeedThat and execute img src
server code var http = require('http'), io = require('socket.io'), fs = require('fs'); respcont =
server.js var io = require('socket.io').listen(3000); io.sockets.on('connection', function (socket) { console.log(server is connected); socket.on('disconnect', function
Server-side: var test; UserModel.findOne({name: 'guest'}, function(err, user) { test = user.name; }); console.log(test); This
Server Errors below: If an error occurred, a HTTP status code 503 (Service Unavailable)
Server works fine, but the problem is the client doesn't receive anything. server.php <?php
server.java ok, so the problem is: When I hit the start button, the gui
Server Tomcat v7.0 Server at 137.57.102.146 was unable to start within 45 seconds. If
server: apache http server 2.2 problem: code does not load my text file hello

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.