Reputation: 17121
I am developing a small app in Node.js. I am just using Node.js for input and output. The actual website is just running through nginx. The website has a Websocket connection with node.js and is primarily used for db manipulations.
One of the things I am trying to do is get node to send small pieces of html along with the data from the database. I tried the following code.
simplified:
connection.on('message', function(message) {
fs.readFile(__dirname + '/views/user.html', function(err, html){
if(err){
console.log(err);
}else{
connection.sendUTF( JSON.stringify({
content: html,
data: {}
}));
}
});
}
});
When I console.log(html)
on the server or in the client I only get numbers back.
Anyone know what could be wrong.
NOTE: I really want to stay away from stuff like socket.io
, express
, etc. Just keeping it as simple as possible and no fallbacks are needed.
Upvotes: 20
Views: 40404
Reputation: 2791
If you don't specify an encoding for fs.readFile, you will retrieve the raw buffer instead of the expected file contents.
Try calling it this way:
fs.readFile(__dirname + '/views/user.html', 'utf8', function(err, html){
....
Upvotes: 48