Radoslav
Radoslav

Reputation: 1812

Node.js htaccess like get rules

I am looking for a way to set dynamic url http://host.com/dynamic_value-123/ and be able to get these values (123)

Upvotes: 0

Views: 1210

Answers (1)

alessioalex
alessioalex

Reputation: 63683

Considering you have the most basic example of a server with Node.js, you can achieve that with req.url.split('-')[1]:

var http = require('http');
http.createServer(function (req, res) {
  console.log(req.url.split('-')[1]);
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World\n');
}).listen(8080);
console.log('Server running at http://127.0.0.1:8080/');

Upvotes: 2

Related Questions