Node.js routing using url module

Node.js routing using url module

Today we are going to learn how to use url node.js module to route Node.js application..We are going to use url.parse method to get the pathname,find  the file and read it using fs.readFile method.Save the data and write is using write() method.


write below file and save it in the same folder containing index.js file.

webpage.html



<!DOCTYPE html>
<html>
<body>
<h1>File number 2</h1>
<p>This is the first file that will be loaded by Node.js</p>
</body>
</html>
webpage2.html

<!DOCTYPE html>
<html>
<body>
<h1>File number 2</h1>
<p>This is the second  file that will be loaded by Node.js</p>
</body>
</html>

write node.js file  and save it index.js 

var http=require('http');
var fs=require('fs');
var url=require('url');

http.createServer(function(req,res)
{
var query=url.parse(req.url,true);
var  filename='.' + query.pathname;
fs.readFile(filename,function(err,data)
{
if(err)
{
res.writeHead(404,{'Content-Type':'text/html'});
res.write("404 Not found error");
return res.end();

}
else
{
res.writeHead(200,{'Content-Type':'text/html'});
res.write(data);
return res.end();

}

});
})
.listen(8080);
console.log('Server running');

Output.




COMMENTS

Name

android arrays cnna1 graphics java java control statements java.swing JButton JFrame JLabel JTextField laravel node.js OPP questions swing technology
false
ltr
item
Code WHIZZ: Node.js routing using url module
Node.js routing using url module
Node.js routing using url module
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh5W9-pZqlN8b7y0DkwWfdYmM3slTSEkVFVG79WWnelOT0CN507rE0ysckSZh5OSiBGsau-OipsOY3cvpv-uxujY0-IiplD-nlXARe0VMwlloM4hVsAhKCqmdgzo423LArDeYHWeQNiL3MK/s320/node.js-url-parse.PNG
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh5W9-pZqlN8b7y0DkwWfdYmM3slTSEkVFVG79WWnelOT0CN507rE0ysckSZh5OSiBGsau-OipsOY3cvpv-uxujY0-IiplD-nlXARe0VMwlloM4hVsAhKCqmdgzo423LArDeYHWeQNiL3MK/s72-c/node.js-url-parse.PNG
Code WHIZZ
https://code-whizz.blogspot.com/2018/11/nodejs-routing-using-url-module.html
https://code-whizz.blogspot.com/
http://code-whizz.blogspot.com/
http://code-whizz.blogspot.com/2018/11/nodejs-routing-using-url-module.html
true
5534598864497432585
UTF-8
Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS CONTENT IS PREMIUM Please share to unlock Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy