Home > Back-end >  nodejs http server command res.end() html tag not recognized
nodejs http server command res.end() html tag not recognized

Time:10-27

Just began backend development and got stuck on html tag not recgnized in node js server.

I have an urgent assignement to create local http server. When using the content type text and json, it works, but with html it doesnt work. Here is the code

const http = require('http');

//CREATE A SERVER wth the HTTP variable
const server= http.createServer(function(req,res)
{
    //headers
    //res.writeHead(200,{"Content-Type": 'text/plain'});
    //res.writeHead(200,{"Content-Type": "application/json"});
    res.writeHead(200,{"Content-Type":'text/html'});

    //send back the response information
    //res.end("wELCOME TO MY zURI INTERNSHIP SERVER!!!");
    //res.end('{"name": "Miaro the great", "College": " ESPA Vontovorona, Antananarivo", "Occupation": "Technical support engineer turned software developer"}');
    res.end('<html><body style="background:blue; text-align: center; color:orange">
    <h1><marquee>Welcome To THIS CLASS YALL</marquee></h1> 
    <p>HOW'S IT GOING?</p>
</body></html>');
});


//creating a port 
server.listen(4000,'localhost');
console.log("GREAT DUDE! YOU HAVE CREATED A SERVER!!!!");

The error is for sure in the function res.end() arguments but I tried '' and "" BUT NOTHING WORKS. Below is an example of error in VS CODE.

S D:\dwz\bosyz\zuri\tasks AND RESSOURCES\backend nodejs\wk05\portfolio> node server.js
D:\dwz\bosyz\zuri\tasks AND RESSOURCES\backend nodejs\wk05\portfolio\server.js:14
    res.end('<html><body style="background:blue; text-align: center; color:orange">
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

SyntaxError: Invalid or unexpected token
    at Object.compileFunction (node:vm:360:18)
    at wrapSafe (node:internal/modules/cjs/loader:1084:15)       
    at Module._compile (node:internal/modules/cjs/loader:1119:27)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1209:10)
    at Module.load (node:internal/modules/cjs/loader:1033:32)    
    at Function.Module._load (node:internal/modules/cjs/loader:868:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
    at node:internal/main/run_main_module:22:47
PS D:\dwz\bosyz\zuri\tasks AND RESSOURCES\backend nodejs\wk05\portfolio>

Tried to change the html tags, the delimiters '' and "", or formatting the texts.

CodePudding user response:

Some error at res.end(). I have corrected it. Use backticks instead of single or double quotes.

const http = require('http');

//CREATE A SERVER wth the HTTP variable
const server= http.createServer(function(req,res)
{
    //headers
    //res.writeHead(200,{"Content-Type": 'text/plain'});
    //res.writeHead(200,{"Content-Type": "application/json"});
    res.writeHead(200,{"Content-Type":'text/html'});

    //send back the response information
    //res.end("wELCOME TO MY zURI INTERNSHIP SERVER!!!");
    //res.end('{"name": "Miaro the great", "College": " ESPA Vontovorona, Antananarivo", "Occupation": "Technical support engineer turned software developer"}');
    res.end(`<html><body style="background:blue; text-align: center; color:orange"><h1><marquee>Welcome To THIS CLASS YALL</marquee></h1><p>HOW'S IT GOING?</p></body></html>`);
});


//creating a port 
server.listen(4000,'localhost');
console.log("GREAT DUDE! YOU HAVE CREATED A SERVER!!!!");

CodePudding user response:

The response should be returned using template literals/ backticks(`) to wrap the html code.

const server= http.createServer(function(req,res)
{
    res.writeHead(200,{"Content-Type":'text/html'});
    res.end(
       `<html>
        <body style="background:blue; text-align: center; color:orange">
            <h1>
                <marquee>Welcome To THIS CLASS YALL</marquee>
            </h1>
            <p>HOW'S IT GOING?</p>
        </body>
        </html>`
    );
});


//creating a port 
server.listen(4000,'localhost');
console.log("GREAT DUDE! YOU HAVE CREATED A SERVER!!!!");

  • Related