0

I have a file named server.js which reads an HTML file named "game.html".

That HTML file is linked with an external javascript file named "game.js".

In server.js file

if(request.url.indexOf('.html') != -1) {
    fs.readFile("game" + request.url, function (error, data) {
        if (error) {
            response.writeHead(404, {"COntent-type":"text/plain"});
            response.end("No Html Page Found.");
        } else{
            response.writeHead(200, {'Content-Type': 'text/html'});
            response.write(data);
            response.end(); 
        }

     });
}
else if(request.url.indexOf('.js') != -1) {
        fs.readFile("game" + request.url, function (error, data) {
            if (error) {
                response.writeHead(404, {"COntent-type":"text/plain"});
                response.end("No Javascript Page Found.");
            } else{
                response.writeHead(200, {'Content-Type': 'text/javascript'});
                response.write(data);
                response.end(); 
            }

        });
    }

In game.html file

<script src="game.js">

In game.js file

function loadXMLDoc(update) {
var fs = require('fs');

if(update)
{
    //Write
    fs.writeFileSync("highscore.txt", s);   
}
else
{
    //Read
    console.log("Reading Highscore.");
    var highscore = fs.readFileSync("highscore.txt");
    console.log("Saved Highscore : " + highscore);
    console.log("Finished Reading.");
}

}

I always get this error while using "var fs = require('fs')" in game.js.

ReferenceError: require is not defined

Any solution to this problem ?

3
  • Your html-file does an include of that external js file, and that uses a require? That won't work. You will need to use the require server-side. Your server.js can do that require. You might want to clear up about what files you use. Commented Aug 3, 2014 at 19:08
  • Missing some details here; you mean server.js is included as a <script> tag? What is the HTML doing in your scripts? Commented Aug 3, 2014 at 19:08
  • Erik Smekens : Is there any solution how I can read from game.js ? I have carried fs operation in server.js successfully but I require file system operation in game.js to read and write . Commented Aug 4, 2014 at 7:58

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.