Node.js
In this tutorial we will learn to parse URL using the Node.js url module.
url
moduleWe can use the url
module for parsing. It is one of the core Node.js module. So, we don't have to install it using NPM.
We write the following to require the url module.
const url = require('url');
We use the parse
method of url
module that helps in parsing url.
Syntax
url.parse(urlString, parseQueryString);
The first parameter is the URL to be parsed. The second parameter is for parseQueryString. If it is true then query property will be set to an object. When it is false, then query property of returned URL object will be unparsed and undecoded string.
In the following example we are going to parse the following URL and also parse the query string.
https://example.com:9000/search?query=HelloWorld&page=1&page_limit=10
To parse the above url we will write the following code.
const url = require('url');
const address = 'https://example.com:9000/search?query=HelloWorld&page=1&page_limit=10';
const parsed = url.parse(address, true);
In the above code parsed
will contain the details of the parsed url.
We can either print it out or pick some of the properties that are saved in the parsed object.
In the following section we are listing out the parsed url data.
{
protocol: 'https:',
slashes: true,
auth: null,
host: 'example.com:9000',
port: '9000',
hostname: 'example.com',
hash: null,
search: '?query=HelloWorld&page=1&page_limit=10',
query: {
query: 'HelloWorld',
page: '1',
page_limit: '10'
},
pathname: '/search',
path: '/search?query=HelloWorld&page=1&page_limit=10',
href: 'https://example.com:9000/search?query=HelloWorld&page=1&page_limit=10'
}
Unparsed query string for the same url.
const url = require('url');
const address = 'https://example.com:9000/search?query=HelloWorld&page=1&page_limit=10';
const parsed = url.parse(address, false);
console.log(parsed);
Output
{
protocol: 'https:',
slashes: true,
auth: null,
host: 'example.com:9000',
port: '9000',
hostname: 'example.com',
hash: null,
search: '?query=HelloWorld&page=1&page_limit=10',
query: 'query=HelloWorld&page=1&page_limit=10',
pathname: '/search',
path: '/search?query=HelloWorld&page=1&page_limit=10',
href: 'https://example.com:9000/search?query=HelloWorld&page=1&page_limit=10'
}
In the following code we are going to create a simple web server using the Node.js core http
module and we will also use the url
module to parse the url.
In the previous tutorial we learned about creating Node.js web server using http module. Feel free to check that out.
const http = require("http");
const url = require("url");
const PORT = 9000;
const server = http.createServer(function (request, response) {
// setting header
const header = { "Content-Type": "application/json" };
// parse the url
const parsed = url.parse(request.url, true);
// for this example we will pick the `message` query
const { pathname, query } = parsed;
const message = query?.message;
// handle /
if (pathname === "/") {
response.writeHead(200, header);
response.write(JSON.stringify({ message: message }));
}
// unknow url
else {
response.writeHead(400, header);
response.write(JSON.stringify({ error: "Bad request" }));
}
response.end();
});
// listen to port 9000
server.listen(PORT);
console.log(`Listening at port ${PORT}`);
The query?.message
checks if query object has message property.
The above code will look for pathname /
and query message
. If message exists then we will get it as response.
Now run the server and visit the url http://localhost:9000
without query message
. This will return us the following response.
{}
If we pass message
query like http://localhost:9000?message=HelloWorld
. This will result in the following response.
{
"message": "HelloWorld"
}
If we try to call something like http://localhost:9000/some-unknown-page?message=HelloWorld
then we will get Bad request error. This happens because we are not handling the some-unknown-page
pathname. So, the else block is executed.
{
"error": "Bad request"
}
ADVERTISEMENT