Question: ========== URL with hostname and port Request message URL: 0.0.1:3000 Response message: SUCCESS! URL with hostname, port and path echo Request message URL: 0.0.1:3000/echo Response
==========
- URL with hostname and port
- Request message URL: 0.0.1:3000
- Response message: SUCCESS!
- URL with hostname, port and path echo
- Request message URL: 0.0.1:3000/echo
- Response message: SUCCESS! echo
- URL with hostname, port, path foxtrot and route parameter value kilo
- Request message URL: 0.0.1:3000/foxtrot/kilo
- Response message: SUCCESS! Received kilo via foxtrot where the value kilo must be retrieved via the route parameter
- Invalid/unexpected URL
- Request message URL: 0.0.1:3000/
- Response message: FAILED! Fix your URL.
- Request message URL: 0.0.1:3000/
--------------------
i did like this
const http = require("http");
const url = require('url');
var hostName = '127.0.0.1'; var port = 3000;
var server = http.createServer(function(req,res){ res.setHeader('Content-Type','text/plain');
const parsed = url.parse(req.url); const pathname = parsed['pathname']
const queryObject = url.parse(req.url,true).query; let queryValue = queryObject['name']
if(queryValue==null){ queryValue="" }
if(pathname == "/echo"){ res.write(`SUCCESS!echo ${queryValue}`); res.end(); }
else if(pathname=="/foxrot"){ res.write("FAILED! Fix your URL"); res.end(); } else{
//for any path run this res.write(`FAILED! Fix your URL.`); res.end(); } })
server.listen(port,hostName,()=>{ console.log(`Server running at http://${hostName}:${port}/`); });
------------------------------------
i think this Request message URL: 0.0.1:3000/foxtrot/kilo is incorrect ,how to write this ?
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
