How to append new line character in node.js?
Asked Answered
W

5

14

I'm new to node.js and I'm trying to write a program that receives http requests and forwards the contents through a socket. At the other end of the socket is a paging system that needs messages terminated with a new line character. So far, it works fine, except there is an extra message sent with the contents: undefined.

When I print the contents of the pager message to the client's browser, there does not appear to be a new line. Am I doing this right?

sys = require("sys"),
http = require("http"),
url = require("url"),
path = require("path"),
net = require("net");

socket = new net.Socket();
socket.connect(4000, '192.168.0.105');

var httpServer = http.createServer(function(request, response) {
    var uri = String(url.parse(request.url).query);
    var message = uri.split("=");
    var page = 'FPG,101,0,3!A' + message[0] + '\n';
    response.writeHead(200, {"Content-Type":"text/html"});
    response.write('sending message: ' + page + " to pager");
    response.end();
    socket.write(page);
}).listen(8080);

sys.puts("Server running at http://localhost:8080/");

EDIT: I narrowed it down further. It turns out that if I do:

var page = 'FPG,101,0,3!A' + 'hello' + '\n';

It works okay. So the output of uri.split("=") must not be what I am expecting.

Wilburwilburn answered 4/5, 2012 at 0:19 Comment(2)
If you watch the traffic with Wireshark, do you see trailing newlines? Do you need to send \r\n instead of just \n?Byrn
No, I got this working in another application by just sending \n, so I know that's all I need.Wilburwilburn
W
-3

I figured out what the problem was. For some reason the browser was sending an additional request to the page right after my request. I just filtered for requests with a certain format and that fixed it.

Wilburwilburn answered 4/5, 2012 at 22:51 Comment(1)
i dont think so. the solution to the issue is what @Duhl has mentioned above.Bub
D
18

I'm sure the new line is there, but you aren't going to see it when you send your Content-Type as text/html. In HTML, \n is just other whitespace, and is treated as such. Use text/plain instead.

Duhl answered 4/5, 2012 at 0:21 Comment(3)
Oh. Okay, well now I'm seeing it on the client's browser. There must be some other problem. Thanks.Wilburwilburn
@ddor254 CSS controls how the whitespace is interpreted. Did you have a specific question?Duhl
yeah , i mean where it is written in the HTML spec that CSS control the representation of the html? and why css is set as interpreter only when the content-type in text/html ?Forrester
M
11

Since content type is 'text/html',we can happily use break statement.Just like this

res.write('hello'+'<br/>');
 res.write('nice to meet you');

You can try this code in node:

var http = require('http');

http.createServer(function (req, res) {

res.writeHead(200, { 'Content-Type': 'text/html' });
res.write('hello,this is saikiran'+'<br/>');
res.end('nice to meet you');

}).listen(process.env.PORT || 8080);
console.log('server running on port 8080');
Mistrot answered 11/12, 2013 at 13:41 Comment(0)
B
0

As per content type of text/plain, you can also use \r to send a return input to the client's browser

Bernat answered 29/7, 2017 at 10:15 Comment(0)
W
-3

I figured out what the problem was. For some reason the browser was sending an additional request to the page right after my request. I just filtered for requests with a certain format and that fixed it.

Wilburwilburn answered 4/5, 2012 at 22:51 Comment(1)
i dont think so. the solution to the issue is what @Duhl has mentioned above.Bub
I
-4

Consider using \\n instead of \n for new line.

Industrialism answered 9/7, 2012 at 10:34 Comment(1)
in general it is true for text file. but here he ask for new line in html format for client browserLampley

© 2022 - 2024 — McMap. All rights reserved.