Tino Caer Tino Caer - 1 year ago 54
Node.js Question

Node.js get value from end of url

Is there a way for me to create a URL like this:

and use
as an id to fetch content? I do not want question marks or other things like this. An example website like this is imgur which doesn't use question marks.

I have looked for other options, but all of their questions requires
in order for it to work.


Answer Source

Use req.params as in express documentation

Route path: /users/:userId/books/:bookId
Request URL: http://localhost:3000/users/34/books/8989
req.params: { "userId": "34", "bookId": "8989" }

In case of using raw http
for using with raw http module you can use str.split("/")[2];

var http = require('http');
function handleRequest(request, response){
    response.end('It Works!! Path Hit: ' + request.url);
    var str = request.url;
    var exampleId = str.split("/")[2];

var server = http.createServer(handleRequest);
server.listen(8080, function(){});