TimGraupner
TimGraupner

Reputation: 63

How to output quotation marks correctly with node.js

I'm trying to write a node.js file to auto-generate some HTML code, and I'm having some trouble with my quotation marks. I tried the escape character, and mixing double and single quotes, but all it's outputting is a bunch of double quotes, and none of the text in my output. How can I get this to work? This is my code, which I'm running at localhost:8080:

var http = require('http');
http.createServer(function (req, res) {
    var i;
    var br = "\n";
    for (i = 1; i <= 227; i++) {
        res.write( br + '<div id="jd' + i + '" ondrop="drop(event)" ondragover="allowDrop(event)"></div>' + br);
    }
    res.end("");

}).listen(8080);

This should be outputting 227 lines such as

<div id="jd1" ondrop="drop(event)" ondragover="allowDrop(event)"></div>

Upvotes: 1

Views: 913

Answers (1)

Mr. Ratnadeep
Mr. Ratnadeep

Reputation: 621

The code you have written works perfectly. To see the rendered div you can add some text inside div so it will visible on browser window.

var http = require('http');
http.createServer(function (req, res) {
    var i;
    var br = "\n";
    for (i = 1; i <= 227; i++) {
        res.write( br + '<div id="jd' + i + '" ondrop="drop(event)" ondragover="allowDrop(event)"></div>' + br);
    }
    res.end("");

}).listen(3000);

Output: Empty divs - See screenshot enter image description here

Try with plain text -

var http = require('http');
http.createServer(function (req, res) {
    var i;
    var br = "\n";
    res.setHeader("Content-Type", "text/plain")
    for (i = 1; i <= 227; i++) {
        res.write( br + '<div id="jd' + i + '" ondrop="drop(event)" ondragover="allowDrop(event)"></div>' + br);
    }
    res.end("");

}).listen(3000);

Output Plain text enter image description here

Upvotes: 1

Related Questions