A
A
Abra Kadabra2016-04-08 01:51:04
Node.js
Abra Kadabra, 2016-04-08 01:51:04

What is the correct command for node.js?

Good evening, I'm trying to get the server to change the request's url, but all to no avail.

var http = require("http");
 
http.createServer(function(req, res) {
 
    // initialize the body to get the data asynchronously
    req.body = "";
 
    // get the data of the body
    req.on('data', function (chunk) {
        req.body += chunk;
        console.log(chunk);
    });
     
    
    //all data
    req.on('end', function () {
       
        if ((id = req.url.match("^/hello/([a-z]+)$"))) {
           if (req.method == "GET") {
                res.writeHead(200, {'Content-Type': 'text/plain'});
                res.end('WE HAVE A WINNER');
            }
        } else {
            res.writeHead(200, {'Content-Type': 'text/plain'});
            if(req.url == ""){ res.end('Hello empty url') } else{
                res.end("Hello wrong input" + req.url);
            }
        }
    });
 
}).listen(8080);

This is what the code looks like.
Everything is fine until it comes to the team.
Can't change req.url using console. What should the team look like?
In the body of the program, everything comes out, I write req.url = "/hello/chtoto"
Everything works, it gives out "WE HAVE A WINNER".

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question