0

I try to put an html in a page in node.js and this is the code:

var http = require('http');
var fs=require('fs');

fs.readFile('C:\Users\Eventi\Desktop\Node.js\Progetti\ProveNodeJS\NodeJSProve\home.html', function (err, html) {
    if (err) {

    }       
    http.createServer(function(request, response) {  
        response.writeHeader(200, {"Content-Type": "text/html"});  
        response.write(html);  
        response.end();  
    }).listen(8000);
});

When I go at the url http://localhost:8000 I obtain the error first argument must be a string or Buffer node js . Anyone can help me?

2 Answers 2

2

The reason is that your readFile() failed and err is set (html is probably undefined). You should fill in your if (err) {} block to do something useful when this is the case.

Also, the most likely reason for the error is that the backslashes are not escaped in your literal filename string. So instead you'll need:

'C:\\Users\\Eventi\\Desktop\\Node.js\\Progetti\\ProveNodeJS\\NodeJSProve\\home.html'
Sign up to request clarification or add additional context in comments.

Comments

1

Escape your path:

fs.readFile('C:\\Users\\Eventi\\Desktop\\Node.js\\Progetti\\ProveNodeJS\\NodeJSProve\\home.html', function (err, html) {

And try again.

Comments

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.