How to add a new line character in node.js?

I am new to node.js and I am trying to write a program that receives http requests and forwards content through a socket. At the other end of the socket there is a paging system that needs messages ending with a new line character. While this works fine, except for the additional message sent to the content: undefined.

When I print the contents of the pager message in the client browser, there seems to be no new line. Am I doing it 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 if I do this:

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

Everything is working fine. Therefore, the conclusion uri.split("=")should not be what I expect.

+9
source share
5 answers

, . - . .

-3

, , , Content-Type text/html. HTML \n . text/plain.

+12

Since the content type is text / html, we are happy to use the break statement. Just

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

You can try this code in the 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');
+8
source

According to the text / plain content type, you can also use \ r to send back input to the client browser

0
source

To use a new line, use \\ninstead \n.

-3
source

All Articles